Reputation: 197
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).
Upvotes: 2
Views: 4165
Reputation: 38130
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.
Upvotes: 2