mockfly logo

Use custom headers

Introduction

There are times when we need to mock an endpoint, but mocking only the response body is not enough; we also need it to return specific headers. We might even want it to return a header with a random value.Mockfly makes this task very easy by allowing us to define the headers we want the endpoint to return, and we can use Faker.js to generate random values.

Define headers:

To define the headers we want the endpoint to return, we simply need to go to the "Headers" tab, where we can add as many headers as we want. Each header has two fields: the header name and the value we want it to have. For example, in the following image, we added a header named Authorization with the value "Bearer 99bde825-6582-44d7-95f9-aaf8143de954":

Mock headersIf we make a request to this endpoint, it will return the Authorization header with the value "Bearer 99bde825-6582-44d7-95f9-aaf8143de954":Mock headers postman response

Use Faker.js to generate random values:

If we want the header value to be a random value, we can use Faker.js. To do this, the header value just needs to be a Faker.js expression. For example, if we want the header value to be a random UUID, we simply need to set Bearer {{string.uuid}} as the header value:

Mock headers with faker.js

And this will return a random value each time we make a request to this endpoint.

Create a project