7

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!

Reinier Kaper
  • 1,079
  • 1
  • 8
  • 17
  • 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 Jan 03 '15 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 Jan 03 '15 at 23:56

7 Answers7

16

Ensure 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 ;)

jobwat
  • 8,527
  • 4
  • 31
  • 30
9

Okay I found it.

Apparently the "comment[response]" is actually:

{"comment":{"response": "something"}}

in JSON.

Learned something :)

Reinier Kaper
  • 1,079
  • 1
  • 8
  • 17
2

Select raw option with JSON(application/json).

enter image description here

mohit uprim
  • 5,226
  • 2
  • 24
  • 28
0

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".

ShadSterling
  • 1,792
  • 1
  • 21
  • 40
0

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 :)

j.krissa
  • 223
  • 5
  • 21
0

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.

Atul Gupta
  • 75
  • 2
  • 10
0

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.

  • 1
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 07 '21 at 19:00