SALES: 1-800-867-1380
Microsoft Azure
Collapse the table of content
Expand the table of content

Content types and headers


Updated: June 16, 2015

Microsoft Azure Mobile Services provides you a great deal of control over the behavior of a custom API endpoint in your mobile service. Each exported custom API function that you implement is passed a request object and response object. These objects are implemented by the express.js library, and they expose the APIs of the express.js library. You can use these APIs to customize the handling of HTTP messages in your custom API.

Unlike table operation scripts that can only accept and return JSON message payloads, a custom API can accept or return any serializable HTTP message payload. This enables you to upload XML content to a custom API or return various formats, including XML and HTML. The following example constructs a simple XML document that is returned as the response payload to a GET request:

exports.get = function(request, response) {
    var xb = require('xmlbuilder');    
    var xml = xb.create('todolist');
    xml.ele('todoitem', { description: "Learn about Custom APIs" });
    xml.ele('todoitem', { description: "Make an app" });

    response.set('Content-Type', 'text/xml');
    response.send(200, xml.end({ indent: true }));

The response payload looks like this:

<?xml version="1.0"?>
    <todoitem description="Learn about Custom APIs"/>
    <todoitem description="Make an app"/>

You can also read and set custom HTTP message headers. The following simple example reads a custom header named my-custom-header and then returns the value in the response:

exports.get = function(request, response) {    
    var header = request.header('my-custom-header');
    response.send(200, "You sent: " + header);

For more information about custom API, see Mobile Services server script reference.

© 2016 Microsoft