如何在URL中显示参数的数组格式?

huangapple go评论67阅读模式
英文:

How to display array format of a parameter in url?

问题

I'm working on NextJS 12 project and I want to pass an array as a parameter to the query.

The format should be like this :

/path?address_id=1235&roofs=[1,2]&lang=en

However, when I pass the array I get an encoded result :

/path?address_id=1235&roofs=%5B1%2C2%5D&lang=en

router.replace({
    pathname: '/panel_placement',
    query: {
        address_id: '11842008',
        roofs:`[${selectedRoofs.join(',')}]`, //here
        lang,
    },
});
英文:

Task

I'm working on NextJS 12 project and I want to pass an array as a parameter to the query.

Expected

The format should be like this :

/path?address_id=1235&roofs=[1,2]&lang=en

Current

However, when I pass the array I get an encoded result :

/path?address_id=1235&roofs=%5B1%2C2%5D&lang=en

Code

router.replace({
					pathname: '/panel_placement',
					query: {
						address_id: '11842008',
						roofs:`[${selectedRoofs.join(',')}]`, //here
						lang,
					},
				});

答案1

得分: 1

不可以直接将数组作为URL参数传递。URL用于标识和定位网络上的资源,遵循特定的格式。URL参数通常表示为由等号(=)分隔并用和号(&)连接的键值对。

虽然不能直接将数组作为URL参数传递,但可以将数组表示为字符串并将其作为参数传递。有几种常见的方法可以实现这一点:

逗号分隔的值:将数组元素转换为逗号分隔的字符串,然后将其作为参数传递。例如:?array=element1,element2,element3。

查询字符串参数:将数组序列化为查询字符串格式,然后将其作为参数传递。例如:?array[]=element1&array[]=element2&array[]=element3。在此方法中,数组表示为具有相同名称的多个参数,后跟方括号[]。

JSON编码:将数组转换为JSON字符串,然后在传递参数之前对其进行URL编码。例如:?array=%5B%22element1%22,%22element2%22,%22element3%22%5D。在这里,%5B表示[,%22表示双引号"在URL编码中。

在服务器端,您需要解析参数值并根据所选的表示方法将其转换回数组。

URL有最大长度限制,因此如果数组很大,可能会遇到URL长度限制的问题。在这种情况下,其他方法,如使用POST请求或以不同格式(例如Base64)编码数组,可能更合适。

请参阅标题为“Uniform Resource Identifier (URI): Generic Syntax”的RFC 3986。

英文:

No, you cannot directly pass an array as a parameter in a URL. URLs are used to identify and locate resources on the web, and they follow a specific format. URL parameters are typically represented as key-value pairs separated by an equal sign (=) and joined with an ampersand (&).

While you cannot pass an array directly as a parameter in a URL, you can represent an array as a string and pass it as a parameter. There are a few common methods to achieve this:

Comma-Separated Values: Convert the array elements into a comma-separated string and pass it as a parameter. For example: ?array=element1,element2,element3.

Query String Parameters: Serialize the array into a query string format and pass it as a parameter. For example: ?array[]=element1&array[]=element2&array[]=element3. In this method, the array is represented as multiple parameters with the same name, followed by square brackets [].

JSON Encoding: Convert the array to a JSON string and URL encode it before passing it as a parameter. For example: ?array=%5B%22element1%22,%22element2%22,%22element3%22%5D. Here, %5B represents [ and %22 represents " in URL encoding.

On the server-side, you will need to parse the parameter value and convert it back into an array based on the chosen representation method.

URLs have a maximum length limit, so if the array is large, you might encounter issues with URL length restrictions. In such cases, other methods like using POST requests or encoding the array in a different format (e.g., Base64) may be more appropriate.

See RFC 3986 titled "Uniform Resource Identifier (URI): Generic Syntax."

huangapple
  • 本文由 发表于 2023年6月13日 03:50:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定