Is there anyway for golang HTTP client,not to escape the requested URL.
For example, a request for URL "/test(a)", gets escaped to "/test%28a%29".
I'm running the code from https://github.com/cmpxchg16/gobench
- 1URL Encoding is one of the responsibilities of the client. And you can decode on the receiving end (or the server already decodes the URL for you).Mihai Stancu– Mihai Stancu2013-12-30 21:00:53 +00:00Commented Dec 30, 2013 at 21:00
- 2I know, the problem is that currently the server doesn't handle encoded urls (i don't have any access to it). For testing purposes, i'm asking if there is anyway to disable the url escaping.Igor Liner– Igor Liner2013-12-30 21:04:49 +00:00Commented Dec 30, 2013 at 21:04
Add a comment |
3 Answers
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("GET", "http://example.com/test(a)", nil) req.URL = &url.URL{ Scheme: "http", Host: "example.com", Opaque: "//example.com/test(a)", } client.Do(req) Example: http://play.golang.org/p/09V67Hbo6H
Documentation: http://golang.org/pkg/net/url/#URL
1 Comment
Edenshaw
Great, it works for me, although the solution was a bit obscure, sorry I mean opaque :p
This appears to be a bug in the URL package:
Comments
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 ( "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 link: https://play.golang.org/p/rOrVzW8ZJCQ