golang – HTTP客户端始终对URL进行转义

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

golang - HTTP client always escaped the URL

问题

有没有办法让golang的HTTP客户端不对请求的URL进行转义呢?例如,对于URL "/test(a)" 的请求,会被转义为 "/test%28a%29"。我正在运行来自https://github.com/cmpxchg16/gobench的代码。

英文:

Is there anyway for golang HTTP client,not to escape the requested URL.<br>
For example, a request for URL "/test(a)", gets escaped to "/test%28a%29".<br>
I'm running the code from https://github.com/cmpxchg16/gobench&lt;br>

答案1

得分: 9

你可以设置一个不透明的URL。

假设你想要的URL指向http://example.com/test(a),你可以这样做:

req.NewRequest("GET", "http://example.com/test(a)", nil)
req.URL = &url.URL{
    Scheme: "http",
    Host:   "example.com",
    Opaque: "//example.com/test(a)",
}
client.Do(req)

示例:http://play.golang.org/p/09V67Hbo6H

文档:http://golang.org/pkg/net/url/#URL

英文:

You can set an opaque url.

Assuming you want the url to point to http://example.com/test(a) you would want to do:

req.NewRequest(&quot;GET&quot;, &quot;http://example.com/test(a)&quot;, nil)
req.URL = &amp;url.URL{
	Scheme: &quot;http&quot;,
	Host:   &quot;example.com&quot;,
	Opaque: &quot;//example.com/test(a)&quot;,
}
client.Do(req)

Example: http://play.golang.org/p/09V67Hbo6H

Documentation: http://golang.org/pkg/net/url/#URL

答案2

得分: 1

这似乎是URL包中的一个错误:

https://code.google.com/p/go/issues/detail?id=6784

https://code.google.com/p/go/issues/detail?id=5684

英文:

This appears to be a bug in the URL package:

https://code.google.com/p/go/issues/detail?id=6784

and https://code.google.com/p/go/issues/detail?id=5684

答案3

得分: 1

你需要将Unescaped Path设置为URL Path,并将Encoded Path设置为RawURL,否则会出现双重转义,例如:

package main

import (
	"fmt"
	"net/url"
)

func main() {

	doubleEncodedURL := &url.URL{
		Scheme: "http",
		Host:   "example.com",
		Path:   "/id/EbnfwIoiiXbtr6Ec44sfedeEsjrf0RcXkJneYukTXa%2BIFVla4ZdfRiMzfh%2FEGs7f/events",
	}

	rawEncodedURL := &url.URL{
		Scheme:  "http",
		Host:    "example.com",
		Path:    "/id/EbnfwIoiiXbtr6Ec44sfedeEsjrf0RcXkJneYukTXa+IFVla4ZdfRiMzfh/EGs7f/events",
		RawPath: "/id/EbnfwIoiiXbtr6Ec44sfedeEsjrf0RcXkJneYukTXa%2BIFVla4ZdfRiMzfh%2FEGs7f/events",
	}

	fmt.Printf("doubleEncodedURL is : %s\n", doubleEncodedURL)
	fmt.Printf("rawEncodedURL is    : %s\n", rawEncodedURL)

}

Playground链接:https://play.golang.org/p/rOrVzW8ZJCQ

英文:

You need to set Unescaped Path to URL Path and Encoded Path to RawURL or else it will double escape e.g.

package main

import (
	&quot;fmt&quot;
	&quot;net/url&quot;
)

func main() {

	doubleEncodedURL := &amp;url.URL{
		Scheme: &quot;http&quot;,
		Host:   &quot;example.com&quot;,
		Path:   &quot;/id/EbnfwIoiiXbtr6Ec44sfedeEsjrf0RcXkJneYukTXa%2BIFVla4ZdfRiMzfh%2FEGs7f/events&quot;,
	}

	rawEncodedURL := &amp;url.URL{
		Scheme:  &quot;http&quot;,
		Host:    &quot;example.com&quot;,
		Path:    &quot;/id/EbnfwIoiiXbtr6Ec44sfedeEsjrf0RcXkJneYukTXa+IFVla4ZdfRiMzfh/EGs7f/events&quot;,
		RawPath: &quot;/id/EbnfwIoiiXbtr6Ec44sfedeEsjrf0RcXkJneYukTXa%2BIFVla4ZdfRiMzfh%2FEGs7f/events&quot;,
	}

	fmt.Printf(&quot;doubleEncodedURL is : %s\n&quot;, doubleEncodedURL)
	fmt.Printf(&quot;rawEncodedURL is    : %s\n&quot;, rawEncodedURL)

}

Playground link: https://play.golang.org/p/rOrVzW8ZJCQ

huangapple
  • 本文由 发表于 2013年12月31日 04:52:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/20847357.html
匿名

发表评论

匿名网友

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

确定