Preface
Whether it is microservices or monolithic architectures, services have time to communicate with each other, and the most direct way of communication isHTTP
Calling, this article will introduceGo
How to do it in languageHTTP
Call and give examples.
Pre-knowledge
HTTP
The call needs to be passedhttp
In the bagClient
In the structureDo
Methods are implemented, so one needs to be declared firstClient
Structural variable, this structure can set timeout and other configurations.
For a requestURL
, query parameters, requestmethod
etc., requiredhttp
In the bagRequest
Structure de-encapsulation. We can passNewRequestWithContext
orNewRequest
Get a basic functionRequest
Structure pointer variable.
NewRequestWithContext(ctx , method, url string, body ) (*Request, error)
- parameter
ctx
forContext
The interface type, arbitrarily implementedContext
All custom types of interfaces can be passed as this parameter. - parameter
method
forHTTP
Method parameters, optional values areGET
、POST
、DELETE
、PUT
wait. - parameter
url
The request path for the interface. - parameter
body
, is the request body parameter.
pass(req)
After the method is called, the return value is(*Response, error)
, the first is the response structure parameter, and the second is the error parameter. Read byResponse
ofbody
The value of , can obtain the response body of the interface.
GET Request
import ( "context" "fmt" "io" "net/http" ) func main() { client := {} request, err := ((), , "http://localhost:8080/user?name=tom", nil) if err != nil { return } ("headerParam", "header") resp, err := (request) if err != nil { (err) return } bytes, err := () if err != nil { return } defer () (string(bytes)) // {"code":0,"data":{"list":[{"name":"Xiao Ming","age":20},{"name":"Xiaohong","age":18}]},"message":"success"}}
- When you need to carry query parameters, you can directly splice them in
url
above the string. -
header
Parameters can be passed through the request structureHeader
Fieldsset
Method oradd
Method to set. - The HTTP request response code can be passed
Response
ofStatusCode
fields for viewing. - After the interface request is successful, pass
Method, read
Response body information.
- Except directly in
url
Splicing onquery
We can also add parameters in the following waysquery
parameter:
params := {} rawUrl, err := ("http://localhost:8080/user") if err != nil { return } ("name", "tom") = () u := ()
passStructural
set
Method setting query parameters,url
passThe function generates a
URL
Structure pointer variable, = ()
Through this line of codequery
Parameters andurl
Make binding, and finally passString()
Method willurl
Convert tostring
type.
summary
This article introduces how to make HTTP calls in Go language, which need to be passedhttp
In the bagClient
Structural variable, call its methodDo
conductHTTP
Before calling HTTP, you need to pass the http packageRequest
Structure encapsulates request path and request parameters. Last passedGET
The request case tellsquery
Parameters andheader
How to set parameters and how to obtain the response body.
This is the end of this article about how to make Http calls in Go. For more relevant content on Http calls in Go, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!