question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass headers in "onResponse" of requests array

See original GitHub issue

I’m trying to test an application which has the server set a cookie in the response, redirects the user to a different path and then checks the for the cookie in the second request. So in short: In step 1 I send a GET to the server which responds with a 302 and a cookie. In step 2 I need go to the location of the 302 and send along the cookie I received in the first request.

I’m using the autocannon requests array to set up the sequence of requests that need to happen.

My problem is that the “onResponse” function doesn’t expose the headers that are returned from the first response. This means I can’t access the “set-cookie” header which in turn means I can’t provide the cookie in the header of the next request

My autocannon setup is like this:


autocannon({
    url: "localhost:3000",
    requests: [
        {
            // Start the request
            method: 'GET',
            path: '/auth,
            onResponse: (status, body, context) => {
            // here I would like to access the "set-cookie" header so I can add it to the context like this:
            // context.cookie = headers["set-cookie"];
            // but no headers are passed to the onResponse method.
                context.path = body.url;
            }
        },
        {
            // Now go the path returned in the previous response and send along the cookie
            method: 'GET',
            setupRequest: (req: any, context: any) => ({
                ...req,
                path: context.path,
                headers: {"cookie": context.cookie}
            }),
            onResponse: (status: number, body: string, context: { [key: string]: any }, headers: any) => {

            }
        }

Are there any ways around this? Is there another way I can use autocannon to achieve what I need?

I checked out the clients API but I don’t think the client.on(“headers”) event helps my case as it doesn’t keep track of where in the request cycle I am. Further it just gives the headers keys and values back as a flat list so I have no idea of knowing where a header stops and the next header begins.

A solution for me would be to pass on the headers in the onResponse method. I can make a PR for this but I’d like to know if there are alternatives first.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
EladIsraelicommented, May 5, 2021

I would like to take this PR 😃

0reactions
Elias-Serneelscommented, Jan 11, 2022

After testing this out it works great in most cases however it doesn’t seem to work when a server sends back multiple headers with the same key but a different value. For example my server sends back multiple set-cookie headers.

The current implementation puts all of the these headers under the same set-cookie key, effectively overwriting the previous value for each header that has the same key.

I think a better solution would be to instead create an array with the values in the case where a server send back multiple headers with the same key.

To summarize current behaviour: when server sends:

"set-cookie": "abc",
"set-cookie": "def",
"set-cookie": "ghi"

results in headers object --> {"set-cookie": "ghi"}

New/expected behaviour: when server sends:

"set-cookie": "abc",
"set-cookie": "def",
"set-cookie": "ghi"

results in headers object --> {"set-cookie": ["abc", "def", "ghi"]}

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to get retrofit request headers onResponse callback
I use call.request.headers() but always returns an empty String[]. All other attributes (url, params, verb) are accessible, only headers are ...
Read more >
Response.headers - Web APIs | MDN
The headers read-only property of the Response interface contains the Headers object associated with the response.
Read more >
Understanding HTTP Requests & Responses in Golang
We can see Header is ultimately a map[string][]string. This is a dictionary or hash map where the keys are strings and the contents...
Read more >
Retrofit — Add Custom Request Header - Future Studio
This tutorial shows you how to add custom headers to your requests by using the @Header annotation within your interface definition or by ......
Read more >
Class: WebRequest - Electron
The filter object has a urls property which is an Array of URL patterns that will be used to filter ... An example...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found