Home  >  Article  >  Backend Development  >  Use the http.Post function to send a POST request and get the response

Use the http.Post function to send a POST request and get the response

PHPz
PHPzOriginal
2023-07-25 08:34:45987browse

Use the http.Post function to send a POST request and get the response

In the Go language, we can use the Post function in the http package to send a POST request and get the response. The Post function is a common function of the http package. It can send form data or json data to the specified URL and return the server's response.

The following is a sample code that demonstrates how to use the http.Post function to send a POST request and get the response:

package main

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

func main() {
    url := "http://example.com/api"
    data := "username=test&password=123456"

    resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data))
    if err != nil {
        fmt.Println("发送POST请求失败:", err)
        return
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("读取响应失败:", err)
        return
    }

    fmt.Println("服务器响应:", string(body))
}

In the above code, we first define a URL and the data. We then use the http.Post function to send a POST request to the specified URL, passing the data and Content-Type. Among them, the second parameter specifies the Content-Type as "application/x-www-form-urlencoded", which means that the data we want to send is a piece of URL-encoded form data. The third parameter is an io.Reader interface, and we use strings.NewReader to convert the data to io.Reader.

The return value of the http.Post function is a pointer to the http.Response structure and a possible error. We first determine whether the error is empty, and if not, print the error message and return it. If there are no errors, we can get the server's response body through resp.Body.

After obtaining the response body, we can use the ReadAll function in the ioutil package to read it into a byte array. Then we convert the byte array to a string and print it.

The above is the sample code that uses the http.Post function to send a POST request and get the response. Through this example, we can learn how to use the Post function in the http package to send a POST request and get the response. In actual development, depending on different interfaces and data formats, we may need to adjust the parameters and processing methods in the code.

The above is the detailed content of Use the http.Post function to send a POST request and get the response. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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