README ¶
rawhttp
rawhttp is a Go package for making HTTP requests. It intends to fill a niche that https://golang.org/pkg/net/http/ does not cover: having complete control over the requests being sent to the server.
rawhttp purposefully does as little validation as possible, and you can override just about anything about the request; even the line endings.
Warning: This is a work in progress. The API isn't fixed yet.
Full documentation can be found on GoDoc.
Example
req, err := rawhttp.FromURL("POST", "https://httpbin.org")
if err != nil {
log.Fatal(err)
}
// automatically set the host header
req.AutoSetHost()
req.Method = "PUT"
req.Hostname = "httpbin.org"
req.Port = "443"
req.Path = "/anything"
req.Query = "one=1&two=2"
req.Fragment = "anchor"
req.Proto = "HTTP/1.1"
req.EOL = "\r\n"
req.AddHeader("Content-Type: application/x-www-form-urlencoded")
req.Body = "username=AzureDiamond&password=hunter2"
// automatically set the Content-Length header
req.AutoSetContentLength()
fmt.Printf("%s\n\n", req.String())
resp, err := rawhttp.Do(req)
if err != nil {
log.Fatal(err)
}
fmt.Printf("< %s\n", resp.StatusLine())
for _, h := range resp.Headers() {
fmt.Printf("< %s\n", h)
}
fmt.Printf("\n%s\n", resp.Body())
PUT /anything?one=1&two=2#anchor HTTP/1.1
Host: httpbin.org
Content-Type: application/x-www-form-urlencoded
Content-Length: 38
username=AzureDiamond&password=hunter2
< HTTP/1.1 200 OK
< Connection: keep-alive
< Server: meinheld/0.6.1
< Date: Sat, 02 Sep 2017 13:22:06 GMT
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
< X-Powered-By: Flask
< X-Processed-Time: 0.000869989395142
< Content-Length: 443
< Via: 1.1 vegur
{
"args": {
"one": "1",
"two": "2"
},
"data": "",
"files": {},
"form": {
"password": "hunter2",
"username": "AzureDiamond"
},
"headers": {
"Connection": "close",
"Content-Length": "38",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org"
},
"json": null,
"method": "PUT",
"origin": "123.123.123.123",
"url": "https://httpbin.org/anything?one=1&two=2"
}
Documentation ¶
Index ¶
- type RawRequest
- type Request
- func (r *Request) AddHeader(h string)
- func (r *Request) AutoSetContentLength()
- func (r *Request) AutoSetHost()
- func (r Request) GetTimeout() time.Duration
- func (r Request) Header(search string) string
- func (r Request) Host() string
- func (r Request) IsTLS() bool
- func (r Request) RequestLine() string
- func (r Request) String() string
- func (r Request) URL() string
- type Requester
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RawRequest ¶
type RawRequest struct { // TLS should be true if TLS should be used TLS bool // Hostname is the name of the host to connect to. E.g: localhost Hostname string // Port is the port to connect to. E.g.: 80 Port string // Request is the actual message to send to the server. E.g: // GET / HTTP/1.1\r\nHost:... Request string // Timeout for the request Timeout time.Duration }
RawRequest is the most basic implementation of Requester. You should probably only use it if you're doing something *really* weird
func (RawRequest) GetTimeout ¶
func (r RawRequest) GetTimeout() time.Duration
GetTimeout returns the timeout for the request
func (RawRequest) IsTLS ¶
func (r RawRequest) IsTLS() bool
IsTLS returns true if the connection should use TLS
func (RawRequest) String ¶
func (r RawRequest) String() string
String returns the message to send to the server
type Request ¶
type Request struct { // TLS should be true if TLS should be used TLS bool // Method is the HTTP verb. E.g. GET Method string // Scheme is the protocol scheme. E.g. https Scheme string // Hostname is the hostname to connect to. E.g. localhost Hostname string // Port is the port to connect to. E.g. 80 Port string // Path is the path to request. E.g. /security.txt Path string // Query is the query string of the path. E.g. q=searchterm&page=3 Query string // Fragment is the bit after the '#'. E.g. pagesection Fragment string // Proto is the protocol specifier in the first line of the request. // E.g. HTTP/1.1 Proto string // Headers is a slice of headers to send. E.g: // []string{"Host: localhost", "Accept: text/plain"} Headers []string // Body is the 'POST' data to send. E.g: // username=AzureDiamond&password=hunter2 Body string // EOL is the string that should be used for line endings. E.g. \r\n EOL string // Deadline Timeout time.Duration }
Request is the main implementation of Requester. It gives you fine-grained control over just about everything to do with the request, but with the posibility of sane defaults.
func FromURL ¶
FromURL returns a *Request for a given method and URL and any error that occured during parsing the URL. Sane defaults are set for all of *Request's fields.
func (*Request) AutoSetContentLength ¶
func (r *Request) AutoSetContentLength()
AutoSetContentLength adds a Content-Length header to the request with the length of Request.Body as the value
func (*Request) AutoSetHost ¶
func (r *Request) AutoSetHost()
AutoSetHost adds a Host header to the request using the value of Request.Hostname
func (Request) GetTimeout ¶
GetTimeout returns the timeout for a request
func (Request) Header ¶
Header finds and returns the value of a header on the request. An empty string is returned if no match is found.
func (Request) RequestLine ¶
RequestLine returns the request line. E.g. GET / HTTP/1.1
type Requester ¶
type Requester interface { // IsTLS should return true if the connection should be made using TLS IsTLS() bool // Host should return a hostname:port pair Host() string // String should return the request as a string E.g: // GET / HTTP/1.1\r\nHost:... String() string // GetTimeout returns the timeout for a request GetTimeout() time.Duration }
A Requester defines the bare minimum set of methods needed to make an HTTP request.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
A Response wraps the HTTP response from the server
func Do ¶
Do performs the HTTP request for the given Requester and returns a *Response and any error that occured
func (Response) Header ¶
Header finds and returns the value of a header on the response. An empty string is returned if no match is found.
func (Response) ParseLocation ¶
ParseLocation parses the Location header of a response, using the initial request for context on relative URLs
func (Response) StatusCode ¶
StatusCode returns the HTTP status code as a string; e.g. 200
func (Response) StatusLine ¶
StatusLine returns the HTTP status line from the response