Reinier Kaper
Reinier Kaper

Reputation: 1129

Postman form-data works, but the raw equivalent doesn't

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!

Upvotes: 8

Views: 42144

Answers (7)

user2708841
user2708841

Reputation: 13

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.

Upvotes: 0

Atul Gupta
Atul Gupta

Reputation: 75

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.

Upvotes: 0

mohit uprim
mohit uprim

Reputation: 5334

Select raw option with JSON(application/json).

enter image description here

Upvotes: 2

j.TK
j.TK

Reputation: 223

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

Upvotes: 0

ShadSterling
ShadSterling

Reputation: 1820

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

Upvotes: 0

jobwat
jobwat

Reputation: 9223

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

Upvotes: 16

Reinier Kaper
Reinier Kaper

Reputation: 1129

Okay I found it.

Apparently the "comment[response]" is actually:

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

in JSON.

Learned something :)

Upvotes: 9

Related Questions