Back

I can not get inputs from a PUT/PATCH request


cesartc posted 3 years ago

I am developing a rest api. I test it with Postman Rest Client. But I have had a headache with PUT and PATCH requests, the sent parameters can not be gotten in the controller with Input::get('param') neither Request::get('param') they always return NULL, I don't understand why. In the store method it works well. PD: I'm not using resourceful controllers (because I thought it could be the problem), I declared it with Route::put()

This is my controller, the store method works well: http://laravel.io/bin/bPb

And here is how I use postman: http://s13.postimg.org/4o0raxufr/postman.jpg

popolla replied 3 years ago

How are you passing the parameters with Postman?
Please post some code.

cesartc replied 3 years ago

This is my controller, the store method works well: http://laravel.io/bin/bPb And here is how I use postman: http://s13.postimg.org/4o0raxufr/postman.jpg

cgoosey1 replied 3 years ago

Laravel cheats with PUT/PATCH etc requests. These need to be POST requests with the extra POST variable '_method' set to the request type (e.g.) PUT.

http://laravel.com/docs/html#opening-a-form (read the read box in this section)

Enjoy!

tlgreg replied 3 years ago

Laravel cheats because html forms only support GET and POST, but it does understand a real PUT/PATCH request.

The problem looks like lies in Symfony it can't parse the data if it's multipart/form-data, as an alternative try using x-www-form-urlencoded content disposition.

cgoosey1 replied 3 years ago

Learn something new every day. cheers @tlgreg

cesartc replied 3 years ago

well, finally I'm not sure about what happens, the put/patch requests go to the right controller method but the inputs couldn't be gotten. I had to use post requests instead of put/patch, breaking the REST best practices :c

PavelPolyakov replied 2 years ago

Is there any update to this question?

I've also tried to use PUT/PATCH, here is the postman: http://screencast.com/t/BNY9sPAhlfrg

However the Input::all() is empty each time.

Any thoughts?

PavelPolyakov replied 2 years ago

ok, it looks like in postman you should point that the data you send is 'x-www-url-formurlencoded'

wired00 replied 2 years ago

Just wondering if this will be "fixed"? I realise you can add an extra form data element "_method" = "PATCH" while posting as POST verb but i'd prefer to just post with verb of PUT or PATCH.

florianbeer replied 2 years ago

Wow, thanks guys for figuring this out! I just spent about an hour trying to find out why my API wont work on PUT/PATCH. My issue was exactly the same with POST working fine but updates had an empty request body when testing via Postman.

I'm glad I finally found this thread and an answer after severely doubting my understanding of Laravel 5's routing and the new FormRequest validation.

canfiax replied 2 years ago

I am having the exact same issue here. Is there any plans to get it fixed?

IAmJulianAcosta replied 2 years ago

PavelPolyakov said:

ok, it looks like in postman you should point that the data you send is 'x-www-url-formurlencoded'

@canfiax Check this

canfiax replied 2 years ago

IAmJulianAcosta said:

PavelPolyakov said:

ok, it looks like in postman you should point that the data you send is 'x-www-url-formurlencoded'

@canfiax Check this

Nice, thanks.

prapats replied 2 years ago

PavelPolyakov said:

ok, it looks like in postman you should point that the data you send is 'x-www-url-formurlencoded'

Thanks a lot

EspadaV8 replied 2 years ago

Anyone worked out how to get this working with PUT/PATCH and form-data? I need to submit files along with the request. For now I've fallen back to using a POST with the _method = PUT but would ideally like a nicer way of dealing with this.

tommthierry replied 2 years ago

POST and PUT use the URL-Params not the form data. Click on the "URL-Params" Button next to select method button.

AndyWendt replied 2 years ago

multipart/form-data via PUT intentionally does not work in php: http://php.net/manual/en/features.file-upload.put-method.php

CharlJKruger replied 2 years ago

tlgreg said:

Laravel cheats because html forms only support GET and POST, but it does understand a real PUT/PATCH request.

The problem looks like lies in Symfony it can't parse the data if it's multipart/form-data, as an alternative try using x-www-form-urlencoded content disposition.

2 days of messing around trying to get it to work and this solved it, thanks!

tucq88 replied 1 year ago

PavelPolyakov said:

ok, it looks like in postman you should point that the data you send is 'x-www-url-formurlencoded'

It solved my problems. Thanks. Postman should has a tooltip or a simple introduction to guide us :D

tucq88 replied 1 year ago

EspadaV8 said:

Anyone worked out how to get this working with PUT/PATCH and form-data? I need to submit files along with the request. For now I've fallen back to using a POST with the _method = PUT but would ideally like a nicer way of dealing with this.

You are my 2nd saviour :d I don't know we could cheat POSTMAN by using that trick This is my case :

  • I want to use method PUT to upload file with POSTMAN. But if I point data to x-www-url-formurlencoded, it would not allow me to use File input. So in this case, with POSTMAN, I use POST, but with a _method input equal to PUT. It will allows me to PUT data with file (This trick only works with Laravel)
marciocamello replied 1 year ago

https://laravel.com/docs/5.2/routing#form-method-spoofing

flik replied 8 months ago

You can check the solution from here: http://rapidsol.blogspot.com/2016/11/how-to-get-inputs-from-putpatch-request.html

It is easy.

CarlosMontiel replied 4 months ago

You only must add ?_method=PUT after your URL request (params)


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us