Parses an HTTP request that contains "application/xml" content.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <sstream>

namespace gcf = ::google::cloud::functions;

gcf::HttpResponse http_xml(gcf::HttpRequest request) {  // NOLINT
  std::istringstream is(request.payload());
  // Use the Boost.PropertyTree XML parser, this is adequate for a small
  // example, but application developers may want to consider a more robust
  // parser for production code.
  boost::property_tree::ptree data;
  boost::property_tree::read_xml(is, data);

  auto name = data.get<std::string>("name", "World");
  return gcf::HttpResponse{}
      .set_header("content-type", "text/plain")
      .set_payload("Hello " + name);


// Package http provides a set of HTTP Cloud Functions samples.
package http

import (

// ParseXML is an example of parsing a text/xml request.
func ParseXML(w http.ResponseWriter, r *http.Request) {
	var d struct {
		Name string
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "Could not read request", http.StatusBadRequest)
	if err := xml.Unmarshal(b, &d); err != nil {
		http.Error(w, "Could not parse request", http.StatusBadRequest)
	if d.Name == "" {
		d.Name = "World"
	fmt.Fprintf(w, "Hello, %v!", html.EscapeString(d.Name))


 * Parses a document of type 'text/xml'
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
exports.parseXML = (req, res) => {
  // Convert the request to a Buffer and a string
  // Use whichever one is accepted by your XML parser
  const data = req.rawBody;
  const xmlData = data.toString();

  const {parseString} = require('xml2js');

  parseString(xmlData, (err, result) => {
    if (err) {


import json
import xmltodict

def parse_xml(request):
    """ Parses a document of type 'text/xml'
        request (flask.Request): The request object.
        The response text, or any set of values that can be turned into a
         Response object using `make_response`
    data = xmltodict.parse(
    return json.dumps(data, indent=2)

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser