What is the difference between URL parameters and query strings?
Asked Answered
O

5

157

I don't see much of a difference between the parameters and the query strings, in the URL. So what is the difference and when should one be used over the other?

Omnipresent answered 1/9, 2016 at 8:55 Comment(3)
Do you have a context, because as far as I know then the two are generally used for the same - but your case might be context specific.Reiser
I don't have a specific context, is a general question. In what case should I use the one way instead of the other. There must be different uses of those two.Omnipresent
Personally - I mostly use the word parameter when taking about them in a variable or method call context, and query string when talking about them in the URL context. (e.g.: the query string is split into parameters for the method). But it's just language so context and situations differ and I doubt anybody would hunt you down for using either one when you "feel" like it :).Reiser
P
126

The query component is indicated by the first ? in a URI. "Query string" might be a synonym (this term is not used in the URI standard).

Some examples for HTTP URIs with query components:

http://example.com/foo?bar
http://example.com/foo/foo/foo?bar/bar/bar
http://example.com/?bar
http://example.com/?@bar._=???/1:
http://example.com/?bar1=a&bar2=b

(list of allowed characters in the query component)

The "format" of the query component is up to the URI authors. A common convention (but nothing more than a convention, as far as the URI standard is concerned¹) is to use the query component for key-value pairs, aka. parameters, like in the last example above: bar1=a&bar2=b.

Such parameters could also appear in the other URI components, i.e., the path² and the fragment. As far as the URI standard is concerned, it’s up to you which component and which format to use.

Example URI with parameters in the path, the query, and the fragment:

http://example.com/foo;key1=value1?key2=value2#key3=value3

¹ The URI standard says about the query component:

[…] query components are often used to carry identifying information in the form of "key=value" pairs […]

² The URI standard says about the path component:

[…] the semicolon (";") and equals ("=") reserved characters are often used to delimit parameters and parameter values applicable to that segment. The comma (",") reserved character is often used for similar purposes.

Phosphorate answered 2/9, 2016 at 14:20 Comment(3)
Parameter and query are different. See sections 3.3 and 3.4 in tools.ietf.org/html/rfc2396.htmlBushwa
@cowlinator: (RFC 2396 is obsolete, but the current standard, RFC 3986, says something similar about parameters in the path component). I didn’t state that they are the same, or did I? URI authors could specify parameters in the query component (as described in my answer), and they could also specify parameters in the path component (as described in your reference) -- in both cases it’s just a convention, nothing that the standard defines. -- Would you suggest a change to my answer? Do you think OP meant parameters in the path?Phosphorate
You can also find the query component at this address: en.wikipedia.org/wiki/Uniform_Resource_Identifier#queryKittie
A
50

Parameters are key-value pairs that can appear inside URL path, and start with a semicolon character (;).

Query string appears after the path (if any) and starts with a question mark character (?).

Both parameters and query string contain key-value pairs.

In a GET request, parameters appear in the URL itself:

<scheme>://<username>:<password>@<host>:<port>/<path>;<parameters>?<query>#<fragment>

In a POST request, parameters can appear in the URL itself, but also in the datastream (as known as content).

Query string is always a part of the URL.

Parameters can be buried in form-data datastream when using POST method so they may not appear in the URL. Yes a POST request can define parameters as form data and in the URL, and this is not inconsistent because parameters can have several values.

I've found no explaination for this behavior so far. I guess it might be useful sometimes to "unhide" parameters from a POST request, or even let the code handling a GET request share some parts with the code handling a POST. Of course this can work only with server code supporting parameters in a URL.

Until you get better insights, I suggest you to use parameters only in form-data datastream of POST requests.

Sources:

What Every Developer Should Know About URLs

RFC 3986

Acetophenetidin answered 6/1, 2017 at 18:23 Comment(1)
Nice clarification! Here's an example with python and its urllib lib: urllib.parse.urlparse( "google.com/…" ) ParseResult(scheme='https', netloc='www.google.com', path='/search', params='', query='q=url+param+vs+query+parameters+python+urllib&newwindow=1', fragment='')Afrit
S
19

Both are the ways of passing data in GET requests. Below are some differences -

Params / Query Parameters / URL Parameters:

https://localhost:3000/user/5896544

access from backend:

request.params.userId = 5896544

Query Strings:

https://localhost:3000/user?userId=5896544

access from backend:

request.query.userId = 5896544

Signpost answered 22/6, 2022 at 6:59 Comment(1)
I don't agree with this. I think your first example is called: Route Parameters or Path Parameters. And your second example is called: Query Parameters or URL Query Parameters or Query String Parameters.Noncooperation
R
2

I guess there is no world consensus on the nomeclature, as I see several articles writen by persons that employs diferent terms for referring the same concept.

So, from my point of view I see things like this:

URL parameter: Any parameter that is present in the URL. They come in several different flawors:

  • Path or Route parameter: value that is part of the URL path.
    • For example: https://stackoverflow.com/questions/39266970 the 39266970 would be the ID of the question
  • Query or String parameter: a pair of key=value that is present after the URL path immediately after the question sign ?
    • For example: https://stackoverflow.com/questions/39266970?slowClient=true, and with that parameter you may program a client computer to perform any specific action for browsers running in slow computers

There is also something called anchor that is quite another parameter as it instruct the browser where to focus on landing on a given page. For example: https://www.i18next.com/translation-function/plurals#interval-plurals (navigate, wait until the page is full loaded and then behold how the browser goes directly to that part of the web page)

Reach answered 30/11, 2022 at 15:50 Comment(0)
O
-2

PATH PARAMS vs QUERY PARAMS

Let say we have base url https://gov.philippines.com, example of path params;

https://gov.philippines.com/sign-up
https://gov.philippines.com/sign-in

So basically path params are the /sign-up or /sign-in, in short, it is the extended path in base-url, while the query params;

https://gov.philippines.com/sign-up?for=membership
https://gov.philippines.com/sign-in?as=admin

The ?for=membership and ?as=admin are the query params wth key and value, that most use case of this, is for filtering the request

Oneupmanship answered 19/5, 2022 at 10:59 Comment(1)
Nope, "gov.philippines.com/sign-up" is path. Parameters can be accessed on server side and are defined in routes by ":". For example /cities/:cityId. Then if client will GET gov.philippines.com/cities/1 server will take "1" by parameter not a path. For example in express.js req.param in route /cities/:cityId and GET request on gov.philippines.com/cities/1 will resolve in { cityId: 1 }Kwok

© 2022 - 2024 — McMap. All rights reserved.