Implementing Advanced Mock Response Data
You can customize your response data by using the JavaScript code such as return request related information, set HTTP code, or response body.
To add the advanced response data:
- Go to the Mock App Details Page for a running Mock app.
- Click the Implementation tab.
- Click Switch to Advanced Responses to add your JavaScript code.
- Move the mouse pointer over the code section, and then click Edit.
-
Enter your JavaScript code.
The following two objects are available to use:
- req Object: use this object to fetch request related data such as path, query parameters and so on.
- res Object: use this object to define the HTTP status code and response body.
var pathparams = req.params; if (pathparams.petId === '1') { res.status = 200; res.body = { id: 1, name: 'cat', status: 'available' }; } else if (pathparams.petId === '2') { res.status = 200; res.body = { id: 2, name: 'dog', status: 'not available' }; } else { res.status = 404; res.body = { error: 'no pet found' }; }
The following code sample shows how to get query parameter name and set response status and code:var name = req.query.name; if (name === 'cat') { res.status = 200; res.body = { id: 1, name: 'cat', status: 'available' }; } else if (name === 'dog') { res.status = 200; res.body = { id: 2, name: 'dog', status: 'not available' }; } else { res.status = 404; res.body = { error: 'no pet found' }; }
- If you want to clear your code, click Revert to Template Code.
- Click Save.
- Click Update Mock app to redeploy the Mock app. You are directed back to the Apps page.
After deploying the Mock app, you can test the app against the mock data. For more details, see Testing an API Mock App.
req
Object
The following table lists the supported req
methods.
Note: The following example shows the response data fetched from the
req
object. Assuming you have a Petstore service /pet/{petId}
, which is defined with the name
and size
query parameters, and the GET request URL is https://cloud.tibco.com:98/pet/1?name=cat&size=small
.API Method | Description | Data Example |
---|---|---|
req.header
|
Parsed request header object. | {"content-type": "application/json; charset=utf-8"}
|
req.params
|
Get path parameters. | { petId: "1" }
|
req.query
|
Get parsed query string. | {name: 'cat', size: 'small'}
|
req.querystring
|
Get raw query string void of the question mark (?) | name=cat&size=small
|
req.href
|
Get full request URL, including protocol , host , and url . |
http://cloud.tibco.com:98/pet/1?name=cat&size=1
|
req.origin
|
Get origin of URL, including protocol and host . |
http://cloud.tibco.com:98
|
req.method
|
Request method. | GET
|
req.body
|
Request body. | {"id": 1, "name": "cat", "status": "available"}
|
req.host
|
Get host(hostname:port) when present. | cloud.tibco.com:98
|
req.hostnanme
|
Get host name when present. | cloud.tibco.com
|
req.search
|
Get a raw query string with the question mark (?). | ?name=cat&size=small
|
req.url
|
Get request URL. | /pet/1?name=cat&size=small
|
req.originalUrl
|
Get request original URL. | /pet/1?name=cat&size=small
|
req.path
|
Get request path name | /pet/1
|
res
Object
The following table lists the supported res
methods.
API Method | Description | Code Example |
---|---|---|
res.status=
|
Set response HTTP code. | res.status=200
|
res.body=
|
Set response body. | res.body = {id: 1, name: 'cat'}
|
res.type=
|
Set response content-type. | res.type=application/json
|
res.header=
|
Set response headers. One or more headers can be set. | res.header['X-Test-Header'] = "helloHeader"
|