Reputation: 57
Hi I just started learning web terms and till now I understand that HTTP uses methods GET, POST, PUT, DELETE to perform CRUD operations. From sites I have read that GET is used to get data from the server while POST is used to send data to the server.
But in forms why do we use both GET and POST to send data of the form to the server? Isn't GET is supposed to get data from the server instead of posting it? What is missing in my understanding?
Upvotes: 2
Views: 139
Reputation: 163262
No matter what method you use, there is always an HTTP request and a response. The methods are for indicating the type of request you're making. POST and PUT requests may also optionally contain request bodies (some information or attachments you're sending along, like form data or a file upload).
Consider the case where you make a GET request:
GET /articles
This asks the server for the /articles
resource. Now, suppose you want to send some parameters. You can include a query string, which is a common convention:
GET /articles?date=2019-12-14
The HTTP protocol itself doesn't really know or care, or specify what the resource is path... it's up to the server to decide how to handle it. In this case, your server may invoke the handler for articles, and query for articles only on the date of December 14, 2019.
By default if you have a form on an HTML page that is using the GET method, then all form parameters will be sent in the query string.
This fine because you're not really trying to PUT or POST data to a particular place. You're actually trying to GET data from a place, and you're just including some extra parameters/instructions to the server on how to handle your request.
To reiterate, no matter what type of request you have, you're always sending some information in the form of your HTTP request. And, the server is always going to send data back in the form of an HTTP response.
Upvotes: 2