0

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

2
  • 1
    URL 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). Commented Dec 30, 2013 at 21:00
  • 2
    I 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. Commented Dec 30, 2013 at 21:04

3 Answers 3

11

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great, it works for me, although the solution was a bit obscure, sorry I mean opaque :p
1

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

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.