# Api cordova ios

**URL:** <https://discourse.slimframework.com/t/api-cordova-ios/2484>\
**Category:** Questions\
**Created:** [June 19, 2018, 7:11am UTC](https://discourse.slimframework.com/t/api-cordova-ios/2484 "2018-06-19T07:11:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abueno](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/abueno/32/699_2.png) [@abueno](https://discourse.slimframework.com/u/abueno)\
**Post date:** [June 19, 2018, 7:11am UTC](https://discourse.slimframework.com/t/api-cordova-ios/2484/1 "2018-06-19T07:11:56Z")

</div>

Hello, I have the following problem ([https://issues.apache.org/jira/browse/CB-14124](https://issues.apache.org/jira/browse/CB-14124)), in Apache they tell me that the problem is with the API.  
In the API I have this and I still have the same problem, any solution?  
[https://prnt.sc/jwlie7](https://prnt.sc/jwlie7)

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 19, 2018, 11:14am UTC](https://discourse.slimframework.com/t/api-cordova-ios/2484/2 "2018-06-19T11:14:12Z")

</div>

Perhaps an HTTP preflight request causes the problem:

> [@Cors Slim Preflight Request Fails](https://discourse.slimframework.com/t/cors-slim-preflight-request-fails/1992/4):
>
> A [preflight](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests) request (which will be OPTIONS rather than POST) must respond with the permission in the response header. Your server needs to respond to it with a 200 OK response and appropriate Access-Control-Allow-... origin headers. More details: [https://stackoverflow.com/a/8689332/1461181](https://stackoverflow.com/a/8689332/1461181)

---

<div class="post-metadata">

**Author:** ![abueno](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/abueno/32/699_2.png) [@abueno](https://discourse.slimframework.com/u/abueno)\
**Post date:** [June 20, 2018, 5:00pm UTC](https://discourse.slimframework.com/t/api-cordova-ios/2484/3 "2018-06-20T17:00:26Z")

</div>

I have added this ([http://prntscr.com/jx81tk](http://prntscr.com/jx81tk)) and it remains the same 😕

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 20, 2018, 7:58pm UTC](https://discourse.slimframework.com/t/api-cordova-ios/2484/4 "2018-06-20T19:58:10Z")

</div>

Could you please try to add this middleware in your `middleware.php`?

```php
use Slim\Http\Request;
use Slim\Http\Response;

// CORS preflight middleware
$app->add(function (Request $request, Response $response, $next) {
    if ($request->getMethod() !== 'OPTIONS') {
        return $next($request, $response);
    }
    $response = $next($request, $response);
    $response = $response->withHeader('Access-Control-Allow-Origin', '*');
    $response = $response->withHeader('Access-Control-Allow-Methods', $request->getHeaderLine('Access-Control-Request-Method'));
    $response = $response->withHeader('Access-Control-Allow-Headers', $request->getHeaderLine('Access-Control-Request-Headers'));
    return $response;
});

```

---

<div class="post-metadata">

**Author:** ![abueno](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/abueno/32/699_2.png) [@abueno](https://discourse.slimframework.com/u/abueno)\
**Post date:** [June 21, 2018, 6:50am UTC](https://discourse.slimframework.com/t/api-cordova-ios/2484/5 "2018-06-21T06:50:14Z")

</div>

> [@odan](#):
>
> Could you please try to add this middleware in your `middleware.php` ?
> 
> ```auto
> // CORS preflight middleware
> $app->add(function (Request $request, Response $response, $next) {
> if ($request->getMethod() !== 'OPTIONS') {
> return $next($request, $response);
> }
> $response = $next($request, $response);
> $response = $response->withHeader('Access-Control-Allow-Origin', '*');
> $response = $response->withHeader('Access-Control-Allow-Methods', $request->getHeaderLine('Access-Control-Request-Method'));
> $response = $resp
> 
> ```

Hello, this is incredible, today I tested and it works correctly with this ([Screenshot by Lightshot](https://prnt.sc/jx81tk))

Maybe it’s the cache.

I am curious and I have tried the middleware.php and it has given me this error:

Type: TypeError  
Message: Argument 1 passed to Closure::{closure}() must be an instance of Request, instance of Slim\Http\Request given  
File: api/src/middleware.php  
Line: 8

Regards, and thank you very much!

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 21, 2018, 8:17am UTC](https://discourse.slimframework.com/t/api-cordova-ios/2484/6 "2018-06-21T08:17:52Z")

</div>

> [@abueno](#):
>
> Type: TypeError

That’s because the namespace definition is missing on top of the php file: `middleware.php`

```php
<?php

use Slim\Http\Request;
use Slim\Http\Response;

//...

```

---

<div class="post-metadata">

**Author:** ![abueno](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/abueno/32/699_2.png) [@abueno](https://discourse.slimframework.com/u/abueno)\
**Post date:** [June 21, 2018, 8:34am UTC](https://discourse.slimframework.com/t/api-cordova-ios/2484/7 "2018-06-21T08:34:28Z")

</div>

ok, now it works, thank you very much for everything!  
a greeting!
