简体   繁体   English

如何处理 RESTful API 中的 PUT 请求?

[英]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 .我想在我的 Web 服务中使用PUT请求进行更新操作,但我不知道如何处理 PUT,因为没有像$_PUT这样的内置变量。

I tried parse_str(file_get_contents("php://input"),$post_vars);我试过parse_str(file_get_contents("php://input"),$post_vars); now if I print_r the $post_vars , it is showing现在,如果我print_r $post_vars ,它会显示

------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?如何获得在发出PUT请求时传递的确切参数?

Your problem你的问题

Your PUT request has multipart/form-data content-type.您的 PUT 请求具有multipart/form-data内容类型。 So parse_str function cannot parse it.所以parse_str函数无法解析它。

Solution解决方案

You must make PUT request with Content-Type=application/x-www-form-urlencoded .您必须使用Content-Type=application/x-www-form-urlencoded发出 PUT 请求。 Then you can parse it with parse_str(file_get_contents("php://input"), $post_vars);然后你可以用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因为文件需要使用multipart/form-data

There is a super global $_SERVER which contains REQUEST_METHOD有一个包含REQUEST_METHOD的超级全局$_SERVER

It can be used to identify the request method used to access the page;可以用来标识访问页面所使用的请求方式; ie 'GET', 'HEAD', 'POST', 'PUT'.即“GET”、“HEAD”、“POST”、“PUT”。

So you can use this at your server to identify the put request.因此,您可以在服务器上使用它来识别放置请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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