# Calling Javascript fetch with POST method to SLIM 4

**URL:** <https://discourse.slimframework.com/t/calling-javascript-fetch-with-post-method-to-slim-4/4504>\
**Category:** Questions\
**Created:** [November 12, 2020, 5:03pm UTC](https://discourse.slimframework.com/t/calling-javascript-fetch-with-post-method-to-slim-4/4504 "2020-11-12T17:03:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hcape](https://avatars.discourse-cdn.com/v4/letter/h/f1d935/32.png) [@hcape](https://discourse.slimframework.com/u/hcape)\
**Post date:** [November 12, 2020, 5:03pm UTC](https://discourse.slimframework.com/t/calling-javascript-fetch-with-post-method-to-slim-4/4504/1 "2020-11-12T17:03:07Z")

</div>

I am trying to access an authentication from a Vue 3-app via my own SLIM 4-application but cannot get the POST-method variables into my SLIM-app.

My call from Vue-app is as follows:

```
const response = await fetch('http://slim4-api.mydomain.net/session/new', {
            method: 'POST',
            body: JSON.stringify({
                email: payload.email,
                password: payload.password,
            })
        });

```

Slim route is as follows:

```
 $app->post('/session/{action}', MySession::class)->setName('session');

```

And the relevant part of Slim 4 app is:

public function handle(ServerRequestInterface $request): ResponseInterface  
{  
$action = $request-\>getAttribute(‘action’, “new”);  
**$post\_data = $request-\>getBody();**  
**$post\_data = $request-\>getParsedBody();**  
$data = [];  
if ($action == “new”) {  
$data[“token”] = $this-\>getToken();  
// $data[“post\_data”] = $post\_data;  
}  
$response = new Response();  
$response-\>getBody()-\>write(json\_encode($data));  
return $response-\>withHeader(‘Content-Type’, ‘application/json’);  
}

The problem is bold statements with $request-\>getParsedBody(); which always gives null as a result. If I output to $this-\>logger-\>info getBody, the JSON-encoding of the POST JSON data prints, but I haven’t found any way to really use the posted data.

According what I have read, the getParsedBody SHOULD be the right way to handle this, but… it gives me nothing…

I am really a long time (I mean really really) PHP user and using json\_decode is something I do in most of these days, but still this mechanism in SLIM 4 overwhelms me like anything.

Could anyone show even a ray of light to this situation which I just don’t get

wbr hank

---

<div class="post-metadata">

**Author:** ![hcape](https://avatars.discourse-cdn.com/v4/letter/h/f1d935/32.png) [@hcape](https://discourse.slimframework.com/u/hcape)\
**Post date:** [November 12, 2020, 7:26pm UTC](https://discourse.slimframework.com/t/calling-javascript-fetch-with-post-method-to-slim-4/4504/2 "2020-11-12T19:26:29Z")

</div>

Hmmm, it seems that

```
file_get_contents('php://input')

```

gives me the POST data I’m looking for…

Still strange that docs talk about getParsedBody()-method…

But anyhow, this seems to work now…

hank

---

<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:** [November 13, 2020, 7:59am UTC](https://discourse.slimframework.com/t/calling-javascript-fetch-with-post-method-to-slim-4/4504/3 "2020-11-13T07:59:23Z")

</div>

The BodyParsing middleware only parses the body of the HTTP request if the `Content-Type` header was transmitted correctly.

Just add this header to make it work:

```auto
// ...
headers: {
    'Content-Type': 'application/json'
},

```

> **[Body Parsing Middleware](https://www.slimframework.com/docs/v4/middleware/body-parsing.html#media-type-detection)**

---

<div class="post-metadata">

**Author:** ![hcape](https://avatars.discourse-cdn.com/v4/letter/h/f1d935/32.png) [@hcape](https://discourse.slimframework.com/u/hcape)\
**Post date:** [November 13, 2020, 9:37am UTC](https://discourse.slimframework.com/t/calling-javascript-fetch-with-post-method-to-slim-4/4504/4 "2020-11-13T09:37:26Z")

</div>

Thank you, that was the (other) missing piece.

I also had to add

```
header("Access-Control-Allow-Headers: Content-Type");

```

to not get the CORS-error, but after that everything works great.

Thank you

hank

---

<div class="post-metadata">

**Author:** ![rpina](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/rpina/32/1388_2.png) [@rpina](https://discourse.slimframework.com/u/rpina)\
**Post date:** [January 12, 2022, 12:46pm UTC](https://discourse.slimframework.com/t/calling-javascript-fetch-with-post-method-to-slim-4/4504/5 "2022-01-12T12:46:31Z")

</div>

Try this json\_decode(app()-\>request-\>getBody(),true)
