0
0
GoHow-ToBeginner · 3 min read

How to Make HTTP POST Request in Go: Simple Guide

In Go, you can make an HTTP POST request using the http.Post function or by creating a custom http.Client and http.NewRequest. The http.Post function sends data to a server and returns the response, making it simple to use for most POST requests.
📐

Syntax

The basic syntax to make a POST request in Go uses the http.Post function:

  • url: The address where you send the request.
  • contentType: The type of data you are sending, like application/json or application/x-www-form-urlencoded.
  • body: The data you want to send, wrapped as an io.Reader.

This function returns a response and an error.

go
http.Post(url string, contentType string, body io.Reader) (*http.Response, error)
💻

Example

This example shows how to send a JSON payload with an HTTP POST request and read the response body.

go
package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
)

func main() {
	url := "https://httpbin.org/post"
	jsonData := []byte(`{"name":"Alice","age":30}`)

	resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	fmt.Println("Response status:", resp.Status)
	fmt.Println("Response body:", string(body))
}
Output
Response status: 200 OK Response body: {"args":{},"data":"{\"name\":\"Alice\",\"age\":30}","files":{},"form":{},"headers":{"Content-Length":"24","Content-Type":"application/json","Host":"httpbin.org"},"json":{"age":30,"name":"Alice"},"origin":"...","url":"https://httpbin.org/post"}
⚠️

Common Pitfalls

Common mistakes when making POST requests in Go include:

  • Not closing the response body with defer resp.Body.Close(), which can cause resource leaks.
  • Sending the wrong Content-Type header that does not match the data format.
  • Not handling errors returned by http.Post or reading the response body.
  • Using strings.NewReader or bytes.NewBuffer incorrectly for the request body.
go
package main

import (
	"bytes"
	"fmt"
	"net/http"
)

func main() {
	url := "https://httpbin.org/post"
	jsonData := []byte(`{"name":"Bob"}`)

	// Wrong: forgetting to close response body
	resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
	if err != nil {
		panic(err)
	}
	// Missing: defer resp.Body.Close()

	fmt.Println("Status code:", resp.StatusCode)

	// Correct way:
	defer resp.Body.Close()
}
Output
Status code: 200
📊

Quick Reference

Tips for making HTTP POST requests in Go:

  • Use http.Post for simple POST requests.
  • Set the correct Content-Type header matching your data.
  • Always defer resp.Body.Close() to free resources.
  • Use bytes.NewBuffer or strings.NewReader to create the request body.
  • Check and handle errors properly.

Key Takeaways

Use http.Post with url, content type, and body to send POST requests in Go.
Always defer closing the response body to avoid resource leaks.
Match the Content-Type header to the data format you send.
Handle errors from the POST request and reading the response.
Use bytes.NewBuffer or strings.NewReader to prepare the request body.