Basic REST API POST Request Using XML


This how-to guide will cover making a POST request to an API using XML data. For retrieving XML data from an API and parsing it out, check out the Basic REST API GET Call Using XML how-to guide.

The Basics

With Google Apps Scripts, you can use a built-in library called UrlFetchApp which contains a method called .fetch(). This .fetch() method accepts two parameters - the first is the URL you want to make the API request against and the second is an object for your "options" which could include the headers options and any other options you need. In this case, you'll want to make sure that the content-type is set to "application/xml", that the method is set to "post", and that you're passing a payload through - payloads are usually going to be an object.

UrlFetchApp.fetch(url, {headers: {"Content-Type": "application/xml"}, method: 'post', payload: payload})

Was it Successful?

After you've made a POST request to an API, there's usually a response to let you know if it was successful. If there isn't one... I'd suggest double-checking to make sure it's provided by a trust-worthy site... Can't ever be to careful with what you're calling in code.. 🙃

In order to read the result of your POST request, you'll need to parse the data. To do that, you can use XmlService.parse() against the API response. Once you have that, you can start working with the individual elements in the response data. For a more in-depth explanation, check out the Basic REST API GET Call Using XML how-to guide.

const response = UrlFetchApp.fetch(url, {headers: {"Content-Type": "application/xml"}, method: 'post', payload: payload}) const responseData = XmlService.parse(response)

Making it Easy to Use

To make life easier, here's a pre-written function that helps you make a POST request to an API where the only thing you need to do is pass through the URL of the API Endpoint and the payload you want to post to the API.

function basicXmlApiPostRequest(url, payload) { const result = XmlService.parse(UrlFetchApp.fetch(url, {headers: {"Content-Type": "application/xml"}, method: 'post', payload: payload})) console.log(result.getRootElement().getChildren().map(element => element.getText())) return result } // // To run it, just call the function and pass in the API Endpoint along with your payload const payload = '<Travelerinformation><name>John</name><email>put_in_a_test_email@example.com</email><address>USA</address></Travelerinformation>' basicXmlApiPostRequest('http://restapi.adequateshop.com/api/Traveler', payload)

Being able to make an API request in Google Apps Script is a powerful tool to have in your arsenal. This basic post request will help you push your Google Sheets data into order applications and databases, as long as you know the endpoint the data should go to and how the payload should be formatted.

Keep in mind that you may need to include an API Key in your url params or an 'Authorization' variable to your headers if the API you're connecting to requires authentication. Some APIs require OAuth2.0 authentication, which is something we cover in our video guide for accessing the Google Search Console API.

thumbnail

NitroGAS Chrome Extension

Want to reference this code snippet right from your Google Apps Script Project? Check out our NitroGAS Chrome Extension. This tool will help you build your scripts faster than you could imagine. The tool itself and access to the code snippets are 100% FREE - Happy Coding!

Get the Extension