Add a Request Body to a POST Request

The API Connector add-on for Google Sheets enables requests using the GET, POST, and PUT methods. In general, GET requests are for retrieving information, POST requests are for creating data, while PUT requests are for updating existing records.

This article describes how to add a request body to a POST (or PUT) request in API Connector.

CONTENTS

CHOOSE THE POST (OR PUT) METHOD

API Connector defaults to the GET method. To make a POST or PUT request, choose that method from the drop-down menu:

post-requests-img1

ENTER A POST BODY

As part of a POST or PUT request, a data payload can be sent to the server in the body of the request. When you select one of those methods from the method drop-down button, the API Connector form changes to display an input field for the post body.

The body contents can be any valid JSON object, for example like this:

{
   "FirstName": "Peter"
   "LastName" : "Piper",
   "UserName" : "ppiper",
   "Email"    : "ppiper@example.com"
 }
post-requests-img2

CREATE POST BODY REQUESTS BASED ON A CELL

You can reference cells in the POST body by wrapping your cell reference in 3 plus signs on either side. (The plus signs tell API Connector that this is a cell reference and not part of the original URL.) So a reference to cell B2 in a tab called QuerySheet would be written like this: +++QuerySheet!B2+++.

Additional details are provided in this article: Create API Request Based on a Cell

SETTING CONTENT-TYPE HEADERS

A content-type header describes the object’s format, so the server knows how to parse it. The default content type will be application/x-www-form-urlencoded. API Connector will use this content type unless you manually set Key = content-type, Value = application/json in the Headers table, like this.

post-requests-img3

Only application/x-www-form-urlencoded and application/json content types are supported.

ADAPTING INSTRUCTIONS FOR CURL

Many services provide documentation on how to make API requests using cURL, a popular command-line tool for interacting with APIs. For example, here is some documentation from Chargebee on how to make a POST request with cURL:

post-requests-img4

In the above screenshot, the ‘-u’ flag marks the credentials that get sent in the Header, while the ‘-d’ flags mark the POST request body. To convert these instructions into a format that works with API Connector, convert all the ‘-d’ values into a JSON object, like this:

{
   "plan_id":"cbdemo_free" ,
   "auto_collection":"off",
   "customer[first_name]":"John" ,
   "customer[last_name]":"Doe" ,
   "customer[email]":"john@user.com" ,
   "billing_address[first_name]":"John" ,
   "billing_address[last_name]":"Doe" ,
   "billing_address[line1]":"PO Box 9999" ,
   "billing_address[city]":"Walnut" ,
   "billing_address[state]":"California" ,
   "billing_address[zip]":"91789" ,
   "billing_address[country]":"US"
}

Enter the above into the POST body input field as described in the Enter a POST Body section.

TROUBLESHOOTING

• If your POST request isn’t working, a likely cause is a mismatched content type. Try adding in a content type into the header as described above, where Key = content-type, and Value = application/json.

• Try running your request with a different API tool like Swagger Inspector to help identify whether the issue lies with API Connector settings or with the API request itself.

• Other possible issues are listed in this article on troubleshooting: Troubleshooting API Requests

8 thoughts on “Add a Request Body to a POST Request”

  1. Ana, even though I appreciate having a POST option available, I don’t see how to take advantage of it considering there’s no actual integration with the google sheets content; maybe I’m missing something? Thanks.

    Reply
    • Hi Oscar, you can send a post request and the resulting response gets printed into your Google Sheet. You can also base the request URL on values in a cell. Can you please let me know what you mean by integration with the Google Sheets content? Maybe you’re referring to basing the request body on a value in a cell? That feature is coming : )

      Reply
  2. Hello, I would like to know how to create a POST request that could do the same that a PUT request in PostMan do, for example, PUT type request in GitHub API.

    Reply
    • Hi there, a POST request can’t do the same thing as a PUT request as they are different methods with different functions. API Connector only supports GET and POST, though I’ll consider adding more methods if I get more demand for it.

      Reply
    • Sorry, there is no way to include a file, you can only enter values directly or reference cells that contain the values you need.

      Reply

Leave a Comment