In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the working process of go requests". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the working process of go requests"?
Github.com/asmcos/requests
At first, I tried to accept the data with map [string] string or map [string] interface {}. I found it troublesome to get the data from the complex object type after acceptance, so I thought of looking at the specific format of the data returned by the interface and constructing the next structure to receive it.
Since the given interface document did not give the correct and error fields [type] of the request token and request data interfaces, the field type of the changed structure was tried several times, but the error message of the request interface could not be simulated, so the Result parameters that can be remembered were added according to the experience of writing python.
The fields contained in the correct and incorrect request interfaces are completely different, but they are all constructed in the accepted structure objects. I wonder if this is the right way to handle them.
Package hrimport ("gitee.com/RandolphCYG/akita/pkg/log"github.com/asmcos/requests") / / HrToken API token returns data structure type HrToken struct {/ / correct AccessToken string `json: "access_token" `ExpiresIn int `json: "expires_in" `Scope string `json: "scope" `TokenType string `json: "token_type" `/ / Code int `json: "code" `Error string` json: "error" `ErrorDescription string `json: "error_description" `Message string` json: "message" `Success bool `json: "success" `} / / the data structure type HrData struct {Content is returned via the HrData HR data API [] HrUser `json: "content" `Empty bool `json: "empty" `Number int `json: "number" `NumberOfElements int `json: "numberOfElements" `Size int `json: "size" `TotalElements int `json: "totalElements" `TotalPages int `json: "totalPages" `/ / when an error occurs Result string `json: "result" `} / / user information structure type HrUser struct {CompanyCode string `json: "company_code" `CompanyName string `json: "company_name" `Name string `json: "ename" `Department string `json: "org_all" `Eid string `json: "pernr" `Stat string `json: "stat2" `Mobile String `json: "usrid" `Mail string `json: "usrid_long" `Title string `json: "zmplans" `} / / HrDataConn HR data model type HrDataConn struct {/ / obtain URL UrlGetToken string `json of token: "url_get_token" gorm: "type:varchar (255) Not null;comment: get the address of token "`/ / URL UrlGetData string `json for obtaining data:" url_get_data "gorm:" type:varchar (255); not null Comment: address for obtaining data: `} / / FetchToken get tokenfunc FetchToken (h * HrDataConn) (token HrToken) {req: = requests.Requests () respFetchToken, err: = req.Post (h.UrlGetToken) if err! = nil {/ / throw error log.Log () .Error ("fetch token failed,err:%v\ n" Err) return} / / deserialize err = respFetchToken.Json (& token) if err! = nil {/ / throw error log.Log () .Error ("convert response to json failed,err:%v\ n" Err) return} if! token.Success {/ / throw wrong log.Log (). Error (token.ErrorDescription) return} return} / / FetchHrData takes token to get HR data func FetchHrData (h * HrDataConn) (hrUsers [] HrUser) {req: = requests.Requests () HrToken: = FetchToken (h) header: = requests.Header {"Authorization": hrToken.TokenType + "" + hrToken.AccessToken "Content-Type": "application/json" Charset=UTF-8 ",} / / send request respFetchData, err: = req.Post (h.UrlGetData, header) if err! = nil {log.Log () .Error (" fetch hr data failed,err:%v\ n " Err) return} var hrdata HrData respFetchData.Json (& hrdata) / / whether the returned data has an error field if hrdata.Result! = "" {log.Log () .Error ("fetch hrdata failed,err%v\ n", hrdata.Result) return} hrUsers = hrdata.Content return} so far I believe you have a deeper understanding of "what is the working process of go requests". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.