- Use case #1: Work with JSON using strings, make a HTTP POST
- Use case #2: Work with JSON using structs, make a HTTP POST
Describe how to change various HTTP request attributes that usually differs from one request to another:
- HTTP headers
- Request timeout
- TODO query string parameters
Do not describe HTTP request/client/transport attributes that usually common for a set of requests:
- Authentication
- HTTP compression
- HTTP proxy
- HTTP redirect policy
- TLS configuration
- Rest of the http.Transport configuration
client := &http.Client{
Timeout: 60 * time.Second,
}
reqBody := `{...}`
req, err := http.NewRequest("POST", "http://example.com/json", strings.NewReader(reqBody))
req.Header.Add("Accept", "application/json")
res, err := client.Do(req)
bytes, err := ioutil.ReadAll(res.Body)
result := string(bytes)
client := &http.Client{
Timeout: 60 * time.Second,
}
reqBody := ReqStruct{}
reqBytes, err := json.Marshal(reqBody)
req, err := http.NewRequest("POST", "http://example.com/json", bytes.NewBuffer(reqBytes))
req.Header.Add("Accept", "application/json")
res, err := client.Do(req)
bytes, err := ioutil.ReadAll(res.Body)
okStruct := OkStruct{}
err = json.Unmarshal(bytes, &okStruct)
reqBody := `{...}`
res, bodyStr, errs := gorequest.New().Timeout(60 * time.Second).
Set("Accept", "application/json").
Post("https://example.com/json").
SendString(reqBody).
End()
reqBody := ReqStruct{}
okStruct := OkStruct{}
res, _, errs := gorequest.New().Timeout(60 * time.Second).
Set("Accept", "application/json").
Post("https://example.com/json").
SendStruct(reqBody).
EndStruct(&okStruct)
reqBody := `{...}`
ro := &RequestOptions{
RequestBody: strings.NewReader(reqBody),
Headers: map[string]string{"Accept": "application/json"},
RequestTimeout: 60 * time.Second,
}
res, err := grequests.Post("https://example.com/json", ro)
result := res.String()
reqBody := ReqStruct{}
ro := &RequestOptions{
JSON: reqBody,
Headers: map[string]string{"Accept": "application/json"},
RequestTimeout: 60 * time.Second,
}
res, err := grequests.Post("https://example.com/json", ro)
okStruct := OkStruct{}
err = res.JSON(&okStruct)
reqBody := `{...}`
client := &http.Client{
Timeout: 60 * time.Second,
}
res, err := sling.New().Client(client).
Set("Accept", "application/json").
Post("https://example.com/json").
Body(strings.NewReader(reqBody)).
Receive(nil, nil)
bytes, err := ioutil.ReadAll(res.Body)
result := string(bytes)
// NOTE Implement ReceiveString(successV, failureV *string) (*http.Response, error) ?
reqBody := ReqStruct{}
client := &http.Client{
Timeout: 60 * time.Second,
}
okStruct := OkStruct{}
errStruct := ErrStruct{}
res, err := sling.New().Client(client).
Set("Accept", "application/json").
Post("https://example.com/json").
BodyJSON(reqBody).
Receive(&okStruct, &errStruct)
reqBody := `{...}`
res, err := resty.R().SetTimeout(60 * time.Second).
SetHeader("Accept", "application/json").
SetBody(reqBody).
Post("https://example.com/json")
result := res.String()
reqBody := ReqStruct{}
res, err := resty.R().SetTimeout(60 * time.Second).
SetHeader("Accept", "application/json").
SetBody(reqBody).
SetResult(OkStruct{}).SetError(ErrStruct{}).
Post("https://example.com/json")
okStruct := response.Result().(*OkStruct)
errStruct := response.Error().(*ErrStruct)
reqBody := `{...}`
res, err := gentleman.New().Request().
URL("http://example.com/json").
Method("POST").
Use(timeout.Request(60 * time.Second)).
Use(headers.Set("Accept", "application/json")).
Use(body.JSON(reqBody)).
Send()
result := res.String()
reqBody := ReqStruct{}
res, err := gentleman.New().Request().
URL("http://example.com/json").
Method("POST").
Use(timeout.Request(60 * time.Second)).
Use(headers.Set("Accept", "application/json")).
Use(body.JSON(reqBody)).
Send()
okStruct := OkStruct{}
err := res.JSON(&okStruct)
Nice one.