Contact Us 1-800-596-4880

Configuring the Response Headers and Status Code

In the HTTP Listener configuration, you can customize the HTTP response body, headers, status code, and reason phrase.

Configure Variable Names

You can configure the default variables that the HTTP listener searches for by configuring these properties in APIkit Router:

  • outboundHeadersMapName

    Name of the variable that contains a headers map added in the response (outboundHeaders by default)

  • httpStatusVarName

    Name of the variable that stores the status code to set in the response (httpStatus by default)

APIkit Router creates both variables and facilitates including custom HTTP headers and status codes in the response.

<apikit:config  name="api-config"
                api="api.raml"
                outboundHeadersMapName="outboundHeaders"
                httpStatusVarName="httpStatus"/>

To modify the default names of these variables, update the default HTTP listener configuration to align the variable names and the configuration.

<http:listener config-ref="api-httpListenerConfig" path="/api/*">
    <http:response
        statusCode="#[vars.httpStatus default 200]">
        <http:headers>#[vars.outboundHeaders default {}]</http:headers>
    </http:response>
    <http:error-response
        statusCode="#[vars.httpStatus default 500]">
        <http:body>#[payload]</http:body>
        <http:headers>#[vars.outboundHeaders default {}]</http:headers>
    </http:error-response>
</http:listener>

Add a Header to a Response

Put the header to add in the outboundHeaders map, as shown in the following example:

<set-variable
    value='#[vars.outboundHeaders default {} ++ {newHeaderName: "Header value"}]'
    variableName="outboundHeaders"/>

Set the Status Code of a Response

Use the following example as a reference to assign a specific value to the httpStatus variable:

<set-variable
    value="201"
    variableName="httpStatus"/>