This is a creation in Article, where the information may have evolved or changed.
Golang provides the official HTTP package, which is very convenient and concise for HTTP operation.
But unlike PHP, using Golang's package to do HTTP operations, or not so "direct", need to instantiate this, instantiate that, a bit like Java
, therefore, in order to write convenient later, the basic request is written here. The next time you use it, copy it directly.
GET request
There are several ways of GET requests
Directly using net/http
function requests within a package
123 |
Import "Net/http"... resp, err: = http. Get ("http://wwww.baidu.com") |
Use the http.client structure to request
1234 |
Import "Net/http"... clt: = http. CLIENT{}RESP, err: = CLT. Get ("http://wwww.baidu.com") |
The most essential way to request
If you take a look at the source code, you will find that both of these methods use the most essential form of the request, using the http.NewRequest
function
12345 |
Req, Err: = http. Newrequest ("GET""http://www.baidu.com"nil)//And then http.client the Do method of the struct body http. Defaultclient can be swapped for another http.clientresp, err: = http. Defaultclient.do (req) |
There are several request methods on the GET request surface of go, but there is only one:
1. Using http.NewRequest
functions to get request
entities
2, using the http.client
structure of the Do
method, the request
entity passed into the Do
method.
POST request
Similar to get requests, there are several ways to post requests, but the http.NewRequest
function and methods are used http.client
in essence Do
.
Using net/http
the Post method of the package band
12345678 |
Import ("net/http""Net/url") ... data: = URL. values{"Start": {"0""offset": {"xxxx"}}body: = Strings. Newreader (data. Encode ()) resp, err: = http. Post ("xxxxxxx" "application/x-www-form-urlencoded", body) |
Or you can
12345678910 |
Import ("Net/http""net/url") ... var r http. Requestr.parseform () r.form.add ("xxx""xxx") Body: = Strings. Newreader (R.form.encode ()) HTTP. Post ("xxxx""application/x-www-form-urlencoded", body) |
If you still feel complex, you can:
1234567 |
Import ("net/http""Net/url") ... data: = URL. values{"Start": {"0""offset": {"xxxx"}}http. Postform ("xxxx", data) |
Post method using the instantiated HTTP client
In fact, the function of directly using the package and the instantiation of the HTTP client is the same, the function of the bottom of the package is simply instantiating a DefaultClient
, and then call the DefaultClient
method. The Post method for using the instantiated HTTP client is given below:
123456789 |
Import ("net/http""Net/url") ... data: = URL. values{"Start": {"0""offset": {"xxxx"}}body: = Strings. Newreader (data. Encode ()) CLT: = http. CLIENT{}RESP, err: = CLT. Post ("xxxxxxx" "application/x-www-form-urlencoded", body) |
And also
1234567891011 |
Import ("Net/http""net/url") ... var r http. Requestr.parseform () r.form.add ("xxx""xxx") Body: = Strings. Newreader (R.form.encode ()) CLT: = http. CLIENT{}CLT. Post ("xxxx""application/x-www-form-urlencoded", body) |
Simple, but limited to form form forms
12345678 |
Import ("net/http""Net/url") ... data: = URL. values{"Start": {"0""offset": {"xxxx"}}clt: = http. CLIENT{}CLT. Postform ("xxxx", data) |
net/http
functions that use packages NewRequest
In fact, whether it is the Get method or the Post method, all get, post HTTP request form, the final will be called the function of the package, a net/http
NewRequest
variety of request forms, but also only the package is different.
1234567891011121314 |
Import ("net/http""Net/url") ... data: = URL. values{"Start": {"0""offset": {"xxxx"}}body: = Strings. Newreader (data. Encode ()) req, err: = http. Newrequest ("POST""xxxxx", body) req. Header.set ("Content-type""application/x-www-form-urlencoded") CLT: = http. CLIENT{}CLT. Do (req) |
Add Request Header
net/http
Packages are not encapsulated directly using a GET or POST method that requests a header, so you can only use the method if you want the header in the request NewRequest
.
123456789101112 |
import ( "net/http" ) ... req, err: = http. Newrequest ( "POST" , , body) / /req can also be written here. Header.set ("User-agent", "MyClient") req. Header.add ( "User-agent" , ) CLT: = http. CLIENT{}CLT. Do (req) |
One thing to note: When adding a header operation, req.Header.Add
and req.Header.Set
all can, but when modifying the operation, can only be used req.Header.Set
.
The two methods are different, golang the implementation of the bottom header is a map[string][]string
, req.Header.Set
method if the original header has no value, then it is no problem, if the value, will replace the original value. And then, on the basis of the req.Header.Add
original value, then append
a value, for example, the value of the original header is "s", I later req.Header.Add
a "a" words, became [s a]
. However, the method to get the header value is req.Header.Get
only the first, so if there is a value, re- req.Header.Add
a new value, the req.Header.Get
resulting value is the same.
Print response response
Golang print response not as good as PHP, eh, compiled language is trouble.
12345678 |
Import ("net/http""Net/url""io/ioutil"string(content) |
Using cookies
Golang provides a packet of cookiesnet/http/cookiejar
12345678 |
... URL, err: = URL. Parse ("https://www.wukong.com/") jar, err: = Cookiejar. New (nil) jar. Setcookies (URL, cookies)//The cookies here are []*http. CookieWUKONGCLT: = http. Client{transport:Nil, Jar:jar}wukongclt.get ("xxxxx") |
The type of the text cookies
[] *http.cookie
can be instantiated by itself, and sometimes can be directly used from the direct to response
resp.Cookies()
get.
Golang's cookie is http.client
associated with http.client
a property that exists as an attribute. Therefore, in order to use cookies in Golang, we must find ways to constructhttp.client
Using proxies
To use HTTP proxy in Golang, you must also construct your own http.client
, and you need to http.client
instantiate a property of the struct Transport
itself.
When using environment variables $http_proxy or $http_proxy as proxies
12345 |
//Get the HTTP proxy address from the environment variable $http_proxy or $http_proxy funcgettransportfromenvironment()(transport *http. Transport) {Transport = &http. Transport{proxy:http. Proxyfromenvironment}return} |
When you build an HTTP proxy using your own
Parameter proxy_addr
is the proxy server IP port number, for example: "http://xxx.xxx.xxx.xxx:6000", note that the "HTTP" must be added
123456789 |
func Gettransportfieldurl (PROXY_ADDR *string) (Transport *http. Transport) {url_i: = URL. Url{}url_proxy, Error: = Url_i.parse (*proxy_addr)ifnil{fmt. PRINTLN (Error. Error ())}transport = &http. Transport{proxy:http. Proxyurl (Url_proxy)}return} |
When using, first call the function, get the corresponding transport
, that is, use GetTransportFieldURL
or GetTransportFieldURL
function, and then build the custom http.client
:
12345 |
Proxyurl: = "http://xxx.xxx.xxx.xxx:6000" Transport: = Gettransportfieldurl (& Proxyurl) CLT: = http. CLIENT{TRANSPORT:TRANSPORT}CLT. Get ( "http://www.baidu.com" ) |