Print

What is an API Request Body?

Contents

  1. What is a request body?
  2. How are request bodies structured?
  3. How to send a request body?

What is a request body?

While GET requests can only fetch data from an API, other types of API request methods (e.g. POST, PATCH, and PUT) can be used to create or update resources on a server.

When making one of these requests, an extra element called a "request body" can be included. For example, when creating a new user account, the request body might include the user's name, email address, and password. The server can then use this information to create the new user account and store it in a database.

While request bodies are usually used to update data on a server, this is not a strict rule, and some APIs instead use request bodies to define the data to be retrieved from their servers.

How are request bodies structured?

Request bodies can be structured in a variety of ways, depending on the specific API and the type of data being sent. These are three common types:

  1. Raw. This can technically hold any kind of string, but generally includes XML or JSON, e.g. {"firstName":"Pied","lastName":"Piper"}
  2. x-www-form-urlencoded. This is used for sending simple text values in a query string, e.g. key1=value1&key2=value2. All characters are url encoded, e.g. spaces are replaced by %20.
  3. Binary. This is used for attaching images, videos, audio, and other non-text files.

Other types include GraphQL and multipart forms. The correct format depends on what the server is expecting, and the API documentation should always tell you how exactly to send data in a way that will be processed correctly.

How to send a request body?

As mentioned above, sending a request body to an API is typically done using the POST, PATCH, or PUT request methods. The body of the request should be formatted according to the specific rules of the API, and then included in the request using the appropriate method for the language or library you're using.

Example - Curl

For example, if you're using the popular command-line tool curl, you can use the -d or --data option to specify the data to include in the body of the request. The curl command would then look like this: curl -X POST https://example.com/api/users -d '{"name":"Pied Piper", "email":"[email protected]", "password":"hunter2"}'

Example - API Connector

If you're instead using a no-code tool like Postman or API Connector, open the request method dropdown and select POST.

Then set up your request, including a request body in the request body section, and click Run to send your POST request.

2 thoughts on “What is an API Request Body?”

Leave a Comment

Jump To