Skip to content

Instantly share code, notes, and snippets.

@jameshd
Last active June 27, 2018 10:02
Show Gist options
  • Save jameshd/891f962eb98f753a85cb0974f0a85b7b to your computer and use it in GitHub Desktop.
Save jameshd/891f962eb98f753a85cb0974f0a85b7b to your computer and use it in GitHub Desktop.
Loosely detecting JSON responses
// console.log(pm.response.headers.has('application/json')); doesn't work, too strict.
const isJsonResponse = (pm.response.headers.filter((header) => header.value.indexOf('application/json') !== -1).length > 0);
if (isJsonResponse) {
pm.test("it should match schema", () => {
const schema = {
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Welcome",
"definitions": {
// ... schema generated at https://app.quicktype.io
}
}
pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;
});
}
@vikkio88
Copy link

vikkio88 commented Jun 20, 2018

const isJsonResponse = pm.response.headers.filter(header => header.value.indexOf('application/json') > -1).length;

giphy

or even

const isJsonResponse = pm.response.headers.filter(({value}) => value.indexOf('application/json') > -1).length;

@jameshd
Copy link
Author

jameshd commented Jun 27, 2018

ohh the deconstructing the object... nice...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment