gpt4 book ai didi

go - http.NewRequest() 解码我的 URL 输入

转载 作者:数据小太阳 更新时间:2023-10-29 03:24:54 26 4
gpt4 key购买 nike

当使用 http.NewRequest("GET", url , nil) 用于包含 % 后跟某个数字的 URL 时,*示例:https://api.deutschebahn.com/freeplan/v1/journeyDetails/356418%252F128592%252F57070%252F90271%252F80%253fstation_evaId%253D8000261) Go 会将字符串编码为 url 中的“/”。我怎样才能避免这种情况?

最佳答案

显式设置 URL 结构的 RawPath 字段:

req := http.NewRequest("GET", "https://api.deutschebahn.com/freeplan/v1/journeyDetails/356418%252F128592%252F57070%252F90271%252F80%253fstation_evaId%253D8000261", nil)
req.URL.RawPath = "/freeplan/v1/journeyDetails/356418%252F128592%252F57070%252F90271%252F80%253fstation_evaId%253D8000261"

这个功能是documented对于这个用例:

Note that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/. A consequence is that it is impossible to tell which slashes in the Path were slashes in the raw URL and which were %2f. This distinction is rarely important, but when it is, code must not use Path directly.

Go 1.5 introduced the RawPath field to hold the encoded form of Path. The Parse function sets both Path and RawPath in the URL it returns, and URL's String method uses RawPath if it is a valid encoding of Path, by calling the EscapedPath method.

关于go - http.NewRequest() 解码我的 URL 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44754171/

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