The injectHeaders
field allows you to modify the request headers before
passing them on to the downstream service. To demonstrate, let's create a mirror imposter
so we can see what headers are sent. This imposter just takes the request
headers it receives and sends them back in the response:
POST /imposters HTTP/1.1
Host: localhost:<%= port %>
Content-Type: application/json
{
"port": 7002,
"protocol": "http",
"name": "Mirror",
"stubs": [{
"responses": [{
"is": { "body": "The body." },
"behaviors": [
{ "decorate": "function (req, res) { res.headers = req.headers; }" }
]
}]
}]
}
Now let's set up another imposter that will proxy requests to the mirror imposter. We'll also set up the inject headers field to insert some custom headers in the outgoing request:
POST /imposters HTTP/1.1
Host: localhost:<%= port %>
Content-Type: application/json
{
"port": 7001,
"protocol": "http",
"name": "Inject Headers",
"stubs": [{
"responses": [{
"proxy": {
"to": "http://localhost:7002",
"injectHeaders": {
"X-My-Custom-Header-One": "my first value",
"X-My-Custom-Header-Two": "my second value"
}
}
}]
}]
}
Then we send a request to our proxy imposter:
GET / HTTP/1.1
Host: localhost:7001
HTTP/1.1 200 OK
Host: localhost:7002
Accept: application/json
Connection: keep-alive
X-My-Custom-Header-One: my first value
X-My-Custom-Header-Two: my second value
Date: Thu, 09 Jan 2014 02:30:31 GMT
Transfer-Encoding: chunked
The body.
Now we can see that the X-My-Custom-Header-One and Two headers were returned back to us, reflected back from the mirror imposter after being injected into the outgoing request.
DELETE /imposters/7001 HTTP/1.1
Host: localhost:<%= port %>
Accept: application/json
DELETE /imposters/7002 HTTP/1.1
Host: localhost:<%= port %>
Accept: application/json