简体   繁体   中英

How to handle PUT requests in RESTful API?

I want to use PUT request for update operation in my web service but I don't know how to handle PUT because there is no inbuilt variable like $_PUT .

I tried parse_str(file_get_contents("php://input"),$post_vars); now if I print_r the $post_vars , it is showing

------WebKitFormBoundaryJnBh3L2DKmMjdVmG Content-Disposition: form-data; name="fruit" vanana ------WebKitFormBoundaryJnBh3L2DKmMjdVmG Content-Disposition: form-data; name="items" 1 ------WebKitFormBoundaryJnBh3L2DKmMjdVmG--

How can I get the exact parameters that is passed while making PUT request?

Your problem

Your PUT request has multipart/form-data content-type. So parse_str function cannot parse it.

Solution

You must make PUT request with Content-Type=application/x-www-form-urlencoded . Then you can parse it with parse_str(file_get_contents("php://input"), $post_vars);

Notes

You won't be able to upload files. Because files require to use multipart/form-data

There is a super global $_SERVER which contains REQUEST_METHOD

It can be used to identify the request method used to access the page; ie 'GET', 'HEAD', 'POST', 'PUT'.

So you can use this at your server to identify the put request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM