Sobes.tech
Senior

What to do if the API returns 200 OK, but the response body does not match the contract?

sobes.tech AI

Answer from AI

If the API returns a status 200 OK, but the response body does not conform to the contract, the following steps should be taken:

  1. Document the discrepancy: record exactly what does not match the contract (structure, data types, required fields, etc.).
  2. Notify the API developers: communicate the issue, include examples of responses and expectations.
  3. Automate contract validation: use contract testing tools (e.g., Pact, Postman tests) to detect such errors early.
  4. Temporarily handle deviations on the client side: if possible, add validation and error handling to prevent failures.
  5. Review the contract: it may be outdated or need clarification.

Example of response validation against the contract in a JavaScript test using Jest:

expect(response.status).toBe(200);
expect(response.body).toHaveProperty('userId');
expect(typeof response.body.userId).toBe('number');
// Additional structure and type checks

Thus, it is important not only to check the status but also to validate the response content according to the contract.

What to do if the API returns 200 OK, but the… - sobes.tech