2

I really want to understand how user input is send via HTTP request object and how its format is determined.

Suppose there are couple of text boxes in the html and when we submit this form using a POST method then generally a query string is formed something like name=tanmay&location=xyz and sent in the HTTP request body. This is all fine !

Now suppose instead of having a querystring I want data to be sent as a json object something like {name:"tanmay",location:"xyz"} then what should I do ? May be set the 'content-type' header to 'application/json' right ? But where can I specify this header in my application.

Is 'content-type' header specified while sending the response to the client ? If that is true, does it means when a HTTP request is constructed then 'content-type' header is read from HTTP response and based on that request body is formatted. Is it true ?

Can someone please provide more information on how HTTP request body is constructed? How a file is sent over an HTTP request.

I know that we have full control when we are creating a response object on server side but it appears I have no control over http request object headers (all we can do it parse it and read data from an http request).

2
  • From what language are you constructing the POST request? Commented Nov 18, 2014 at 12:56
  • I believe this question is more on HTTP request/response model rather than based on language ? Please let me know if I am wrong. BTW, I am using MEAN stack for my development. Commented Nov 18, 2014 at 13:02

1 Answer 1

2

HTTP merely provides a method for having a POST body, and how Content Types should be sent.

The actual mechanism for constructing a request is up the the user agent, which might be a web browser, but could just as easily be a mobile phone app, etc.

Without using JavaScript, web browsers typically support application/x-www-form-urlencoded, multipart/form-data or text/plain (specified using the enctype attribute on the form). Of course, by using JavaScript code running on the client browser, this could format as virtually anything the code likes, and send it across, including JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

so here is my understanding : when a form is sent to browser then based on 'enctype' it will format the data in the request body and sent it back to server (we can use enctype="application/json" to format data into a JSON object). By default enctype is 'application/x-www-form-urlencoded' in this body is passed as a regular queryString. Using forms in angular has a different effect though. In angular by default POST requests have Content-Type:application/json hence on submitting a form body is formatted as a JSON object rather than a query string. Please correct me if I am wrong.
That's because angular is a JavaScript framework running on the client, and formatting the post body as it likes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.