gpt4 book ai didi

Ruby URI 合并和转义

转载 作者:数据小太阳 更新时间:2023-10-29 08:06:09 25 4
gpt4 key购买 nike

我有一个 URI 对象。

require 'uri'
uri = URI.parse 'http://example.com'

我需要将一些路径合并到这个 URI。

path = URI.escape('/?foo[123]=5')
uri.merge!(path)

并得到异常:

URI::InvalidURIError: bad URI(is not URI?): /?foo[123]=5

这是因为路径中的某些符号被认为是不安全的(“[”和“]”),但这些符号没有被 URI.escape 转义。

我正在使用两个 URI.escape 调用解决这个问题:

path = URI.escape(URI.escape('/?foo[123]=5'), '[]')
uri.merge!(path)

问题是:为什么 URI.escape 默认不执行转义?

或者也许有更好的方法来做到这一点?

最佳答案

它的规范查看 issue #3457 on ruby bugtracker

关于Ruby URI 合并和转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5065733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com