Ashish
Ashish

Reputation: 81

How to simulate sending form data in a POST API Call through POSTMAN

I am trying to simulate a POST call from a mobile app using POSTMAN. I did the following:

  1. Verb : POST
  2. Params: added 2 params (both show up in the URL)
  3. Header: added key-value pair - Content-Type / application/json
  4. Body: selected 'form-data' radio button and added 2 key-value pairs to represent the form input - email / [email protected] & age / 18

When I submit the request and in my server code (PHP) I print the $request object I only get the params (step #2) and not the form field.

Wondering whether POSTMAN is not passing the form data or I am not looking at the right place (ie in $request object).

I also tried with 'raw' instead of 'form-data' and passed a JSON, but same result.

I tried calling the same API from the web application, I get proper form-data values in the $request object.

Any help to resolve the issue I am facing is much appreciated. I have spent almost whole day going through POSTMAN doc and searching on google but not much headway.

Upvotes: 3

Views: 19362

Answers (1)

Quentin
Quentin

Reputation: 943630

Verb : POST

Correct

Params: added 2 params (both show up in the URL)

You probably shouldn't do that

If you are making a POST request, put them in the 'form data' on the body tab. (It is possible for a POST request to include query string data, but it is unusual).

Header: added key-value pair - Content-Type / application/json

Definitely don't do that.

You are sending form encoded data, not JSON.

Postman will set the correct content-type automatically if you don't override it.

Body: selected 'form-data' radio button and added 2 key-value pairs to represent the form input - email / [email protected] & age / 18

Probably correct. The default encoding for forms is x-www-form-urlencoded, but PHP can handle both.

I only get the params (step #2) and not the form field.

PHP doesn't have native support for parsing HTTP requests with a JSON encoded message body so it sees your claim that you are sending JSON and does nothing.

If it did support JSON for that, then it would try to parse the form encoded data as JSON and fail.

I also tried with 'raw' instead of 'form-data' and passed a JSON, but same result.

You have to manually decode JSON encoded request bodies.

Upvotes: 4

Related Questions