I have an API that I'm testing with and if I submit my data through "form-data" with the following values it works:
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:
{ "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.
Any help would be appreciated!
I have an API that I'm testing with and if I submit my data through "form-data" with the following values it works:
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:
{ "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.
Any help would be appreciated!
Share Improve this question asked Jan 3, 2015 at 23:25 Reinier KaperReinier Kaper 1,1291 gold badge8 silver badges19 bronze badges 2- Do you understand that JSON and www-form-urlencoded are actually different formats? I doubt there is a way to force server to parse JSON encoded payload from the client side. – Yury Tarabanko Commented Jan 3, 2015 at 23:30
- 2 Yes, I'm not using x-www-form-urlencoded though, I'm using form-data in Postman, which is JSON ;-) That's the reason I'm so confused. There must be a header that's off or something. – Reinier Kaper Commented Jan 3, 2015 at 23:56
7 Answers
Reset to default 16Ensure your Content-Type
header!
If your raw data is 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 JSON was selected in the side dropdown...
Hope it saves time to the next one ;)
Okay I found it.
Apparently the "comment[response]" is actually:
{"comment":{"response": "something"}}
in JSON.
Learned something :)
Select raw option with JSON(application/json).
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".
I too had an API and if I submit my data through "form-data" with the following values it works:
key: survey_answer[answers_json]
value: {"2456-9876-4ff7-9807-4097ed21be57":"testing from postman-today "}
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"}}}
and If your raw data is JSON: set Content-Type: application/json
Hope this helps :)
In my case the url was localhost/index.php?userId=1
- and I was using raw mode with { "username":"abc" }
Then, I converted the url to localhost/index.php?userId=1&username=abc
- it is working for GET
and for POST
with the following json: { "userId":"1","username":"abc" }
and url localhost/index.php?userId=1&username=abc
.
Hopefully this helps someone but in my case the method drop down was doing a GET instead of a POST. Change it to POST and you should be good to go.