Sending Data with POST Requests in Go

Sending Data with POST Requests

Welcome to this lesson on sending data with POST requests using Go's net/http package. As you continue your exploration of interacting with RESTful APIs, you'll learn how to send data to a server using the POST method. POST requests are essential when you want to create new resources or submit data, such as filling out a web form or adding a new entry to a database. Unlike GET requests, which allow you to retrieve data, POST requests transmit data to an API.

Understanding these differences is crucial as you expand your skill set in HTTP methods. Let’s dive deeper into utilizing POST requests to comprehend how they stand apart from GET requests.

Key Differences Between GET and POST

Before diving into POST requests, let's briefly compare them to GET requests:

  • GET Requests:

    • Purpose: Retrieve data from a server.
    • Data Location: Data is sent in the URL as path or query parameters.
    • Success Status: Expect a http.StatusOK status code for successful data retrieval.
  • POST Requests:

    • Purpose: Send data to a server to create or update a resource, such as submitting a form, uploading a file, or adding a new item to a database.
    • Data Location: Data is sent in the request body.
    • Success Status: Expect a http.StatusCreated status code for successful resource creation.

These differences clarify when to use each method. POST requests, in particular, require careful handling of the request body.

Understanding the Request Body

For POST requests, the request body is crucial as it holds the data you want to send to the server. This data is usually structured in formats like JSON or XML, with JSON being a common choice due to its readability and compatibility.

Here’s an example of a JSON request body represented as a string:

jsonBody := `{
  "title": "Learn Go http requests",
  "description": "Complete a course on Go API calls.",
  "done": false
}`

This represents a new todo item, including a title, completion status, and description. Noticeably, we are not sending an id, as this is typically managed by the server upon resource creation.

Crafting a Post Request: Adding a New Todo

Let's walk through an example of how to craft a POST request to add a new todo item to our API using Go's net/http package.

First, we will need to prepare the data we wish to send. Here, we'll be adding a new todo item with a specific title, a completion status, and a description. The data is represented as a JSON string:

// New todo data structured as JSON string
jsonBody := `{
  "title": "Learn Go http requests",
  "description": "Complete a course on Go API calls.",
  "done": false
}`

// Base URL for the API
baseURL := "http://localhost:8000"

// Complete endpoint URL
endpoint := baseURL + "/todos"

// Send the POST request
resp, err := http.Post(endpoint, "application/json", bytes.NewBufferString(jsonBody))
if err != nil {
    log.Fatalf("Failed to send POST request: %v", err)
}
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal