In addition to the fact that data can be passed to the server via the body, which is used for POST requests and sending forms, we can also pass the so-called query string. These are the key=value parameters that are located in the request line after specifying the verbs POST or GET.
POST /login?key=value HTTP/1.1
It's worth mentioning that there may be no pointer to a specific page, and the parameters may be passed to the domain's main page.
GET /?key=value HTTP/1.1
Query string have the same format as strings in POST request bodys only a
? sign is placed at the beginning. After the query string, we can pass the parameters
key=value&key2=value2 and so on.
query string parameters have nothing to do with GET-requests, although many developers call them GET-parameters. At interviews, you might be asked, “Is it possible to send POST and GET parameters at the same time?". The correct answer is to say that it is indeed possible, since there's no connection between them. Moreover, in some languages there are ways to get these data separately from each other. For example, in PHP there is a global
$_REQUEST, array, which includes both parameters passed via query string, and those passed in POST.
But how do you understand when to use query string and what for? You need to determine what the HTTP verbs actually mean. The semantics of HTTP verbs is expressed not only in what kind of response will be returned to you, but also in how you can work with it. The verb GET is considered idempotent. This means that executing it multiple times always returns the same result, i.e., it's a normal query. We ask the system about something, it returns data to us, and doesn't change its internal state. Thus, any repeated request will work in the same way, it's deterministic. We can use a pure function as an analogy. In reality, of course, the state of the system can change, and we can't control it, so caching isn't a trivial task.
A POST request, on the contrary, is not idempotent and is intended to change data. Thus, making the request again may lead to different results. If we delete data using the first POST - request, then we'll get a 404 error with the second one. These requests are never cached.
Search robots visit sites using GET - links, since they take semantics into account and know that GET requests can be made painlessly and nothing terrible will happen to the site.
Query string parameters in GET requests are used when working with forms that are of at least two types: for changing, creating or adding data, and for selecting them. It's when developing the second type of form that the query string is used. It could be some type of filtering in a search form. If we type in a search engine, for example, a word and click on Search, then we will see that we will be sent to a page whose URI will be sent to query string. This is a very important point, because this is what makes sure we don't make any changes, and we can give out a link to this request. It's very important to adhere to the semantics of verbs. For example, in some old banking systems, requests were implemented via POST for some reason . With this approach, it's not only impossible to give a link to the request, but even simply refreshing the page with F5 leads to data being resent.
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.