3

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 / someone@gmail.com & 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.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
Ashish
  • 81
  • 1
  • 3
  • 4
  • 1
    Content type application/json and posting a form are exclusive, if you want to post form datas the content-type should be application/x-www-form-urlencoded – Tensibai May 17 '16 at 12:19

1 Answers1

4

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 / someone@gmail.com & 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.

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Hi Quentin, thanks for quick response. I removed the header and params as you suggested and submitted the request. Now I get a blank array when I do `$request->all()`. Earlier same would return the 2 params. – Ashish May 17 '16 at 12:28
  • Tried below code on on the server side while passing data as both raw & x-www-form-urlencode `$inputJSON = file_get_contents('php://input'); $input= json_decode( $inputJSON, TRUE ); dd(json_encode($input));` Output is "" Not sure what wrong I am doing? – Ashish May 17 '16 at 12:55