Recipes

HTTP methods

Specify an HTTP method in the manifest request block. For example, create a route to handle the POST method. Here is an example:

...
rules:
  request:
    path: /my-new-api
    method: POST
...    

The following methods are supported:

  • GET

  • HEAD

  • POST

  • PUT

  • PATCH

  • DELETE

  • OPTIONS

  • TRACE

Path variables

A braced string in the manifest request block is treated as a path variable. For example, create /users/{userId} for handling /users/1, /users/2 and so on. Here is an example:

...
rules:
  request:
    path: /users/{userId}
  response:
    body:
     name: User${path.userId}  

Response header

You can set pairs of key and value to the headers. The value should preferably be a string (an integer is allowed) and can be parsed as a pattern (see also the later section).

...
rules:
  ...
  response:
    headers:
      content-type: text/plain
      x-uuid: "1234567890"

You can set multiple values.

...
rules:
  ...
  response:
    headers:
      set-cookie:
        - sessionId=38afes7a8
        - id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT

Response body

You can serve a text body as follows:

...
rules:
  ...
  response:
    headers:
      content-type: application/xml
    body: |
      <?xml version="1.0" encoding="UTF-8"?>
      <users>
        <user>
          <id>1</id>
          <name>Foo</name>
        </user>
      </users>

You can serve a JSON body as follows:

...
rules:
  ...
  response:
    headers:
      content-type: application/json
    body:
      id: 1
      name: Alice

If a character set is specified in the content-type header, the response body is encoded to the character set.

...
rules:
  ...
  response:
    headers:
      content-type: text/plain;charset=Shift_JIS
    body: あいうえお

Files

You can serve a file content as follows:

...
rules:
  ...  
  response:
    headers:
      content-type: image/jpeg
    file: photo.jpg

You can store the larger response bodies in a separate file.

...
rules:
  request:
    path: /users/{userId}
  response:
    headers:
      content-type: application/json
    file: big-file.json
#big-file.json
{
  "id": "${path.userId}",
  "name": "Oliver",
  ...
  "account": "YU89RE00"
}

Last updated