Skip to main content

Matching requests by their payload

A URL and a method are often not enough to tell requests apart. Every GraphQL query goes to the same /graphql, and a search endpoint answers every filter on the same URL. The Request payload tab is how a rule picks the requests it means by their body.

The payload is a filter​

On the Request payload tab, the editor holds a filter: a rule only catches a request whose body matches it. Leave it empty and the rule catches every request on its URL and method, whatever the body.

How the body has to match is picked in the bar above the editor, Body: Equals | Contains:

ModeCatches
Equals (default)only a body strictly equal to the payload
Containsany body that includes the fields you list, whatever else it has

The sentence next to the switch always says which mode is active, and while the payload is not empty the tab carries an equals or contains chip, so you can tell from any tab how a rule matches.

Equals is free, Contains is a paid feature

Contains is only available on paid plans (see our plans). On the free plan it is shown with a 💎 and opens the upgrade prompt.

On a mock rule​

A mock rule never sends the request, so its payload does one thing only: decide which requests get the mocked response.

a mock rule catching every getCity query, whatever its variables

With Contains and the payload { "operationName": "getCity" }, this rule mocks every getCity query, whatever variables it carries:

{ "operationName": "getCity", "variables": { "id": "100" } }
{ "operationName": "getCity", "variables": { "id": "345" } }

A getCountry query on the same /graphql goes through untouched. With Equals it would only catch a body that is exactly { "operationName": "getCity" }, which a real GraphQL client never sends, so Contains is usually what you want here. One rule per operation, all on the same URL, is how you mock a GraphQL API one query at a time (see GraphQL).

On a modify rule​

A modify rule calls the server, so its Request payload tab has two editors side by side:

  • Filter by is the same filter as on a mock rule, with the same Equals or Contains switch. It decides which requests the rule catches.
  • Replace by is the body sent to the server instead, once a request matched. Leave it empty to send the original body.

a modify rule filtering getCity queries and replacing the body they send

Here every getCity query matches, whatever city it asked for, and goes out to the server asking for city 345 instead. The two editors are independent: the filter never changes what is sent, and Replace by never changes what matches. Replace by swaps the whole body, so write the complete payload you want the server to receive, not only the fields that change. A GET or HEAD request has no body, so Replace by does nothing on one.

tip

To change a single field and keep the rest of whatever the page sent, use a request hook instead. To change one request by hand, just once, use Edit before send.

Things to know​

  • Under Contains, {} and [] match every JSON body. tweak warns you in the bar until you add the fields to match on.
  • A form post (application/x-www-form-urlencoded) is matched field by field, in any order, under both modes. See form request bodies.
  • Variables work in both editors.


Was this page helpful?

Need something else? Request a feature