getParsedBody all String

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.

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?

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.

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

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

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

$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.

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

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).
1 Like

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

1 Like

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.

1 Like

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

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

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 … :man_facepalming:

thanks all for the help.

1 Like