What is the difference between URL parameters and query strings? Ask Question

What is the difference between URL parameters and query strings? Ask Question

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?

ベストアンサー1

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:

[…] クエリ コンポーネントは、多くの場合、「キー = 値」ペアの形式で識別情報を伝達するために使用されます […]

² URI標準ではパスコンポーネントについて:

[…] セミコロン (";") と等号 ("=") の予約文字は、そのセグメントに適用可能なパラメータとパラメータ値を区切るためによく使用されます。 カンマ (",") の予約文字も同様の目的でよく使用されます。

おすすめ記事