Javascript 邮递员表单数据有效,但原始等价物无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27760672/
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
Postman form-data works, but the raw equivalent doesn't
提问by Reinier Kaper
I have an API that I'm testing with and if I submit my data through "form-data" with the following values it works:
我有一个正在测试的 API,如果我通过“表单数据”使用以下值提交我的数据,它会起作用:
key: response[comment]
value: This is a test
But if I do some custom JSON in the "raw" tab with the following structure, it doesn't work:
但是,如果我在具有以下结构的“原始”选项卡中执行一些自定义 JSON,则它不起作用:
{ "response[comment]": "This is a test" }
It's driving me nuts to be honest as the server doesn't give me any details on what's wrong. I have the feeling it's the encoding of the object that goes wrong, but I'm using Angular and I get the same 400 error, while I'm fairly sure it should just work with a JS object as the data on a .post.
老实说这让我发疯,因为服务器没有给我任何关于错误的细节。我感觉它是对象的编码出错了,但我使用的是 Angular 并且我得到了相同的 400 错误,而我相当确定它应该只与 JS 对象一起使用作为 .post 上的数据。
Any help would be appreciated!
任何帮助,将不胜感激!
回答by jobwat
Ensure your Content-Typeheader!
确保您的Content-Type标题!
If your raw data is JSON: Content-Type: application/json
如果您的原始数据是 JSON: Content-Type: application/json
I experienced a similar issue to yours: my request was working using the form-data, but not as raw... and after debugging the request server side, I realised my request Content-Type was "text/plain;charset=UTF-8".. even if JSONwas selected in the side dropdown...
我遇到过类似的问题对你的:我的要求是使用工作表单数据,但不作为原料......和调试请求服务器端后,我意识到了我的请求的Content-Type是"text/plain;charset=UTF-8"..即使JSON是在选择侧面下拉...
Hope it saves time to the next one ;)
希望它可以节省下一个的时间;)
回答by Reinier Kaper
Okay I found it.
好的,我找到了。
Apparently the "comment[response]" is actually:
显然“评论[响应]”实际上是:
{"comment":{"response": "something"}}
in JSON.
在 JSON 中。
Learned something :)
学到东西了:)
回答by Atul Gupta
In my case the url was localhost/index.php?userId=1- and I was using raw mode with { "username":"abc" }
在我的情况下,网址是localhost/index.php?userId=1- 我使用的是原始模式{ "username":"abc" }
Then, I converted the url to localhost/index.php?userId=1&username=abc- it is working for GETand for POSTwith the following json: { "userId":"1","username":"abc" }and url localhost/index.php?userId=1&username=abc.
然后,我将 url 转换为localhost/index.php?userId=1&username=abc- 它GET适用POST于以下 json:{ "userId":"1","username":"abc" }和 url localhost/index.php?userId=1&username=abc。
回答by ShadSterling
In my case, the one form-data entry was being turned into a header, and wasn't included in the body at all. It also worked to set the header explicitly rather than entering form-data. The Content-Type and JSON formatting from other answers were not involved. I would not expect arbitrary form-data keys to work this way; the only key being used in this case was "ID".
就我而言,一个表单数据条目被转换为标题,并且根本不包含在正文中。它还可以明确设置标题而不是输入表单数据。不涉及其他答案中的 Content-Type 和 JSON 格式。我不希望任意的表单数据键以这种方式工作;在这种情况下使用的唯一键是“ID”。
回答by j.krissa
I too had an API and if I submit my data through "form-data" with the following values it works:
我也有一个 API,如果我通过“表单数据”使用以下值提交我的数据,它会起作用:
key: survey_answer[answers_json]
键:survey_answer[answers_json]
value: {"2456-9876-4ff7-9807-4097ed21be57":"testing from postman-today "}
值:{"2456-9876-4ff7-9807-4097ed21be57":"今天邮递员测试"}
and I tried to convert it to raw data, so finally I could fix this by
我试图将其转换为原始数据,所以最后我可以通过
{"survey_answer":{"answers_json": {"2ed4e729-c1fa-4ff7-9807-4097ed21be57":"test from tessa"}}}
{"survey_answer":{"answers_json": {"2ed4e729-c1fa-4ff7-9807-4097ed21be57":"来自泰莎的测试"}}}
and If your raw data is JSON: set Content-Type: application/json
如果您的原始数据是 JSON:设置 Content-Type: application/json
Hope this helps :)
希望这可以帮助 :)


