Skip to content

How to work with cookies in insomnia.sendRequest (Pre-Request and Post-Request scripts)? #7791

Answered by ihexxa
andreygrigoriev asked this question in Help
Discussion options

You must be logged in to vote

Hi @andreygrigoriev , thanks for trying scripts.

  1. Nope, currently cookies are persisted only after script execution completion, so you might maintain cookie values manually.
  2. Yes, although it is a bit complicated, will try to simplify it:
const { Cookie } = require('insomnia-collection');
const cookies = [
	new Cookie({key: 'myCookie1', value: 'abc'}),
	new Cookie({key: 'myCookie2', value: 'def'}),
];

const rawReq = {
    url: 'httpbin.org/anything',
    method: 'POST',
    header: {
	'Cookie': Cookie.unparse(cookies),
        'Content-Type': 'text/plain',
    },
    body: {
        mode: 'raw',
        raw: 'rawContent',
    },
};

await insomnia.sendRequest(
	rawReq,
	(error, response)…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@andreygrigoriev
Comment options

Answer selected by andreygrigoriev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants