Home > Backend Development > Golang > How to send a GET request and parse the response using HTTP client functions in Go language?

How to send a GET request and parse the response using HTTP client functions in Go language?

WBOY
Release: 2023-07-29 10:30:54
Original
1544 people have browsed it

How to send a GET request and parse the response using HTTP client functions in Go language?

1. Introduction to HTTP client functions
In the Go language, the standard library provides functions for implementing HTTP clients. These functions can be used to send various types of HTTP requests and parse the responses. In this article, we will focus on how to use HTTP client functions to send a GET request and parse the response.

2. Send a GET request
In the Go language, sending a GET request can be implemented through the http.Get() function. The following is a simple example:

package main

import (
    "fmt"
    "log"
    "net/http"
    "io/ioutil"
)

func main() {
    resp, err := http.Get("https://api.example.com/data")
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(string(body))
}
Copy after login

In the above code, we send a GET request by calling the http.Get() function. This function returns a response object resp and a possible error err. We use the defer statement to ensure that the response body is closed before the function exits.

3. Parse the response
In the previous step, we read all the data from the response body through the ioutil.ReadAll() function and stored it in a byte arraybody中. Typically, we need to parse the response data into a specific data structure for further processing.

The following is an example of parsing response data into JSON format:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

type Data struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

func main() {
    resp, err := http.Get("https://api.example.com/data")
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }

    var data Data
    err = json.Unmarshal(body, &data)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("ID: %d
", data.ID)
    fmt.Printf("Name: %s
", data.Name)
}
Copy after login

In the above code, we define a Data structure containing ID Two fields, and Name. By calling the json.Unmarshal() function, the response data is parsed into a Data structure object data. Finally, we print out the field value of data.

The above are the basic steps and sample code for using the HTTP client function in the Go language to send a GET request and parse the response. Through these functions, we can easily handle HTTP requests and responses and further process the required data.

The above is the detailed content of How to send a GET request and parse the response using HTTP client functions in Go language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template