# getParsedBody all String

**URL:** <https://discourse.slimframework.com/t/getparsedbody-all-string/4259>\
**Category:** Questions\
**Created:** [June 30, 2020, 4:08pm UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259 "2020-06-30T16:08:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![goldrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/goldrak/32/823_2.png) [@goldrak](https://discourse.slimframework.com/u/goldrak)\
**Post date:** [June 30, 2020, 4:08pm UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/1 "2020-06-30T16:08:38Z")

</div>

Hello,  
I don’t understand why the getParsedBody function converts all the json parameters to string, not respecting the types that are sent and change interger or null to string.

Is there a way I can avoid this behavior?

Thank you.  
A greeting.

---

<div class="post-metadata">

**Author:** ![l0gicgate](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@l0gicgate](https://discourse.slimframework.com/u/l0gicgate)\
**Post date:** [June 30, 2020, 4:42pm UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/2 "2020-06-30T16:42:58Z")

</div>

Slim 3? Slim 4?

Can you show a sample request stub including headers and payload?

`getParsedBody()` attempts to detect content type and uses `json_decode()` it’s pretty straight forward.

Which PSR implementation are you using?

---

<div class="post-metadata">

**Author:** ![goldrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/goldrak/32/823_2.png) [@goldrak](https://discourse.slimframework.com/u/goldrak)\
**Post date:** [June 30, 2020, 5:58pm UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/3 "2020-06-30T17:58:49Z")

</div>

Hi @l0gicgate,  
Thanks for your interest and help.

I use slim 4 and slim/psr7 implementation.

I send this.

```
curl 'http://localhost/api/pacientes' \
  -H 'Connection: keep-alive' \
  -H 'DNT: 1' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Accept: */*' \
  -H 'Origin: http://localhost' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Referer: http://localhost/view/pacientes/new' \
  -H 'Accept-Language: es-ES,es;q=0.9,en;q=0.8' \
  -H 'Cookie: i18n_redirected=es; adminer_permanent=; adminer_key=ec230ef7c5d2e9d0448d76df5c6c79b4; adminer_settings=; adminer_version=4.7.7; adminer_sid=3e1c195b79dbfcc4acf4f0fc05bc1560' \
  --data-raw 'nombre=Demo%20nombre&apellidos=apellidos%20demo&numeroHistoria=434576798709776&direccion=Calle%20Falsa&ciudad=MAdrid&provincia=Madrid&codigoPostal=12334&telefono=98127841&movil=null&dni=null&seguridadSocial=null&fechaNacimiento=null&sexo=0&raza=0&peso=null&talla=null&profesion=null&medicoReferencia=null&situacionLaboral=0&gradoActividad=0&gradoUrgencia=0&aseguradora=null' \
  --compressed

```

Recived this in the gerparsedbody()

**array** _(size=22)_ ‘nombre’ =\> string ‘Demo nombre’ _(length=11)_ ‘apellidos’ =\> string ‘apellidos demo’ _(length=14)_ ‘numeroHistoria’ =\> string ‘434576798709776’ _(length=15)_ ‘direccion’ =\> string ‘Calle Falsa’ _(length=11)_ ‘ciudad’ =\> string ‘MAdrid’ _(length=6)_ ‘provincia’ =\> string ‘Madrid’ _(length=6)_ ‘codigoPostal’ =\> string ‘12334’ _(length=5)_ ‘telefono’ =\> string ‘98127841’ _(length=8)_ ‘movil’ =\> string ‘null’ _(length=4)_ ‘dni’ =\> string ‘null’ _(length=4)_ ‘seguridadSocial’ =\> string ‘null’ _(length=4)_ ‘fechaNacimiento’ =\> string ‘null’ _(length=4)_ ‘sexo’ =\> string ‘0’ _(length=1)_ ‘raza’ =\> string ‘0’ _(length=1)_ ‘peso’ =\> string ‘null’ _(length=4)_ ‘talla’ =\> string ‘null’ _(length=4)_ ‘profesion’ =\> string ‘null’ _(length=4)_ ‘medicoReferencia’ =\> string ‘null’ _(length=4)_ ‘situacionLaboral’ =\> string ‘0’ _(length=1)_ ‘gradoActividad’ =\> string ‘0’ _(length=1)_ ‘gradoUrgencia’ =\> string ‘0’ _(length=1)_ ‘aseguradora’ =\> string ‘null’ _(length=4)_

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![darkalchemy](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@darkalchemy](https://discourse.slimframework.com/u/darkalchemy)\
**Post date:** [June 30, 2020, 8:03pm UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/4 "2020-06-30T20:03:42Z")

</div>

If you want the raw data, use getBody() instead.

---

<div class="post-metadata">

**Author:** ![goldrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/goldrak/32/823_2.png) [@goldrak](https://discourse.slimframework.com/u/goldrak)\
**Post date:** [July 1, 2020, 9:47am UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/5 "2020-07-01T09:47:53Z")

</div>

I try use getBody() and recived this

**object** (_Slim\Psr7\Stream_)[_158_]  
_protected_ ‘stream’ =\> **resource** (_116_, _stream_)  
_protected_ ‘meta’ =\> null  
_protected_ ‘readable’ =\> null  
_protected_ ‘writable’ =\> null  
_protected_ ‘seekable’ =\> null  
_protected_ ‘size’ =\> null  
_protected_ ‘isPipe’ =\> null  
_protected_ ‘finished’ =\> null  
_protected_ ‘cache’ =\>  
**object** (_Slim\Psr7\Stream_)[_177_]  
_protected_ ‘stream’ =\> **resource** (_112_, _stream_)  
_protected_ ‘meta’ =\>  
**array** _(size=6)_  
‘wrapper\_type’ =\> string ‘PHP’ _(length=3)_  
‘stream\_type’ =\> string ‘TEMP’ _(length=4)_  
‘mode’ =\> string ‘w+b’ _(length=3)_  
‘unread\_bytes’ =\> int 0  
‘seekable’ =\> boolean true  
‘uri’ =\> string ‘php://temp’ _(length=10)_  
_protected_ ‘readable’ =\> null  
_protected_ ‘writable’ =\> boolean true  
_protected_ ‘seekable’ =\> boolean true  
_protected_ ‘size’ =\> null  
_protected_ ‘isPipe’ =\> boolean false  
_protected_ ‘finished’ =\> null  
_protected_ ‘cache’ =\> null

If you tell me where is the data I can use this method.

Thanks

---

<div class="post-metadata">

**Author:** ![darkalchemy](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@darkalchemy](https://discourse.slimframework.com/u/darkalchemy)\
**Post date:** [July 1, 2020, 10:12am UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/6 "2020-07-01T10:12:32Z")

</div>

You’re correct, I should have included getContents().

```auto
$request->getBody()->getContents();

```

will return the body content, untouched, as a string. You would then have to do the extraction to an array.

But, I think using getParsedBody() would be cleaner and faster, even if you have to cast int’s, etc.

Here’s the [documentation](http://www.slimframework.com/docs/v3/objects/request.html).

getParsedBody() will use 1 of these methods to extract the body.

```auto
JSON requests are converted into associative arrays with json_decode($input, true).
XML requests are converted into a SimpleXMLElement with simplexml_load_string($input).
URL-encoded requests are converted into a PHP array with parse_str($input).

```

---

<div class="post-metadata">

**Author:** ![kAlvaro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/kalvaro/32/730_2.png) [@kAlvaro](https://discourse.slimframework.com/u/kAlvaro)\
**Post date:** [July 1, 2020, 1:56pm UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/7 "2020-07-01T13:56:00Z")

</div>

> [@goldrak](#):
>
> `application/x-www-form-urlencoded`

`application/x-www-form-urlencoded` doesn’t have anything to do with JSON :-?

---

<div class="post-metadata">

**Author:** ![l0gicgate](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@l0gicgate](https://discourse.slimframework.com/u/l0gicgate)\
**Post date:** [July 1, 2020, 1:57pm UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/8 "2020-07-01T13:57:35Z")

</div>

If you want types to be preserved than you should be using JSON from you client. You need to send a JSON body and use the appropriate content type header `Content-Type: application/json`

Using url encoded parameters will not yield the results that you’re after.

---

<div class="post-metadata">

**Author:** ![goldrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/goldrak/32/823_2.png) [@goldrak](https://discourse.slimframework.com/u/goldrak)\
**Post date:** [July 1, 2020, 2:03pm UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/9 "2020-07-01T14:03:58Z")

</div>

When I send with Content-Type: application/json I reviced this :S

```
/var/www/src/Controller/Pacientes/Create.php:17:null
```

---

<div class="post-metadata">

**Author:** ![goldrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/goldrak/32/823_2.png) [@goldrak](https://discourse.slimframework.com/u/goldrak)\
**Post date:** [July 1, 2020, 2:19pm UTC](https://discourse.slimframework.com/t/getparsedbody-all-string/4259/10 "2020-07-01T14:19:21Z")

</div>

Now work correctly, I have restarted the apache and it works I don’t know because it gives me the problem now it was from the cache … 🤦‍♂️

thanks all for the help.
