Upon performing an HTTP request using the Requests library, a Response
struct is returned, encapsulating all vital information from the HTTP response. This includes the status code, headers, and the response body.
- Accessing Basic Response Information
- Examining Headers and Cookies
- Content Handling
- Parsing Response Body
- Storing Response Content
- Evaluating Response Success
The Response
struct provides several methods to access basic information about the HTTP response:
-
StatusCode and Status: Obtain the HTTP status code and the corresponding status message.
statusCode := response.StatusCode() status := response.Status()
-
Header: Retrieve headers from the response.
contentType := response.Header().Get("Content-Type")
-
Cookies: Access the cookies provided in the response.
cookies := response.Cookies() for _, cookie := range cookies { fmt.Printf("Cookie: %s Value: %s\n", cookie.Name, cookie.Value) }
-
Body and String: Get the response body either as raw bytes or a string.
bodyBytes := response.Body() bodyString := response.String()
Headers and cookies are essential for many HTTP interactions, and the Response
struct simplifies their access:
- Use
Header()
for reading response headers. - Use
Cookies()
to fetch cookies set by the server.
The Response
struct offers methods to facilitate handling of different content types:
-
ContentType: Determine the
Content-Type
of the response.if response.ContentType() == "application/json" { fmt.Println("Received JSON content") }
-
IsJSON, IsXML, IsYAML: Check if the content type matches specific formats.
if response.IsJSON() { fmt.Println("Content is of type JSON") }
By leveraging the Scan
, ScanJSON
, ScanXML
, and ScanYAML
methods, you can effortlessly decode responses based on the Content-Type
.
Given a JSON response, you can unmarshal it directly into a Go struct using either the specific ScanJSON
method or the generic Scan
method, which automatically detects the content type:
var jsonData struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Unmarshal using ScanJSON
if err := response.ScanJSON(&jsonData); err != nil {
log.Fatalf("Error unmarshaling JSON: %v", err)
}
// Alternatively, unmarshal using Scan
if err := response.Scan(&jsonData); err != nil {
log.Fatalf("Error unmarshaling response: %v", err)
}
For XML responses, use ScanXML
or Scan
to decode into a Go struct. Here's an example assuming the response contains XML data:
var xmlData struct {
Name string `xml:"name"`
Age int `xml:"age"`
}
// Unmarshal using ScanXML
if err := response.ScanXML(&xmlData); err != nil {
log.Fatalf("Error unmarshaling XML: %v", err)
}
// Alternatively, unmarshal using Scan
if err := response.Scan(&xmlData); err != nil {
log.Fatalf("Error unmarshaling response: %v", err)
}
YAML content is similarly straightforward to handle. The ScanYAML
or Scan
method decodes the YAML response into the specified Go struct:
var yamlData struct {
Name string `yaml:"name"`
Age int `yaml:"age"`
}
// Unmarshal using ScanYAML
if err := response.ScanYAML(&yamlData); err != nil {
log.Fatalf("Error unmarshaling YAML: %v", err)
}
// Alternatively, unmarshal using Scan
if err := response.Scan(&yamlData); err != nil {
log.Fatalf("Error unmarshaling response: %v", err)
}
For saving the response body to a file or streaming it to an io.Writer
:
-
Save: Write the response body to a designated location.
// Save response to a file if err := response.Save("downloaded_file.txt"); err != nil { log.Fatalf("Failed to save file: %v", err) }
To assess whether the HTTP request was successful:
-
IsSuccess: Check if the status code signifies a successful response.
if response.IsSuccess() { fmt.Println("The request succeeded.") }