php 如何正确格式化发布请求的正文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11146268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:51:04  来源:igfitidea点击:

How to properly format body for post request?

phpjqueryjsonrestpost

提问by Rob

I'm using a firefox pluing called restclient to simulate a post request. It doesnt' seem to be picking up any post data, but not sure if I'm formatting it properly.

我正在使用名为 restclient 的 Firefox 插件来模拟发布请求。它似乎没有获取任何帖子数据,但不确定我是否正确格式化。

using header: Content-Type: application/json and body: {"id":1234}

使用标题:内容类型:应用程序/json 和正文:{"id":1234}

but not go, it's not picking up the id parameter in my php, is there some special formatting I need to set?

但不去,它没有在我的php中获取id参数,是否需要设置一些特殊格式?

回答by Rob

okay, got it working, here is what is needed

好的,让它工作,这是需要的

two content types:

两种内容类型:

Content-Type: application/json
Content-Type: application/x-www-form-urlencoded

and then set your params like so in body:

然后在正文中像这样设置你的参数:

param1=value1&param2=value2

Thanks for the help everyone.

谢谢大家的帮助。

回答by lanzz

PHP will not parse a JSON body automatically into the $_POSTsuperglobal. That only happens with application/x-www-form-urlencodedand multipart/form-dataPOST bodies. That said, you canparse the body yourself — you can access the raw POST body via the php://inputpseudo-stream.

PHP 不会自动将 JSON 主体解析为$_POST超全局变量。这只发生在application/x-www-form-urlencodedmultipart/form-dataPOST 正文中。也就是说,您可以自己解析正文——您可以通过php://input伪流访问原始 POST 正文。