Home > Backend Development > Golang > How to make HTTP POST request using Golang?

How to make HTTP POST request using Golang?

PHPz
Release: 2024-06-04 09:31:57
Original
570 people have browsed it

Using Go to make HTTP POST requests requires: importing HTTP packages; creating HTTP requests; setting request headers; sending requests; and processing responses.

如何使用 Golang 进行 HTTP POST 请求?

How to make an HTTP POST request using Go

Making an HTTP POST request in Go is a common task that allows clients The client sends data to the server. This article walks you step-by-step through the process of making POST requests using Go.

Step 1: Import the HTTP package

First, you need to import the HTTP package, which provides HTTP functionality in Go.

import "net/http"
Copy after login

Step 2: Create HTTP Request

Next, create a new HTTP request using the http.NewRequest function. This function accepts a request method, request URL, and optional HTTP body.

req, err := http.NewRequest("POST", "https://example.com/api/endpoint", body)
if err != nil {
    // 处理错误
}
Copy after login

Step 3: Set the request header

Set the request header as needed. Here is an example of setting the Content-Type header:

req.Header.Set("Content-Type", "application/json")
Copy after login

Step 4: Send the request

Use http.Client to send the request.

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
    // 处理错误
}
Copy after login

Step 5: Process the response

Process the response and read the response body from the resp.

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
Copy after login

Practical Case

The following is a complete example that demonstrates how to use Go to send a POST request to an API endpoint on localhost:

package main

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

func main() {
    // 创建要发送的数据
    data := map[string]interface{}{
        "name": "John Doe",
        "age":  30,
    }
    jsonBytes, err := json.Marshal(data)
    if err != nil {
        // 处理错误
    }

    // 创建请求
    req, err := http.NewRequest("POST", "http://localhost:8080/api/create", bytes.NewReader(jsonBytes))
    if err != nil {
        // 处理错误
    }

    // 设置内容类型头
    req.Header.Set("Content-Type", "application/json")

    // 发送请求
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        // 处理错误
    }
    defer resp.Body.Close()

    // 处理响应
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // 处理错误
    }

    // 打印响应主体
    fmt.Println(string(body))
}
Copy after login

The above is the detailed content of How to make HTTP POST request using Golang?. 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