Search code examples
typescriptrequestplaywrightplaywright-typescript

How to get "Location" response header using playwright?


Problem: I try to get all response headers using playwright request, but there no "Location" header. Could you help me how i can resolve the problem?

My code:

const pw = await request.newContext({
    ignoreHTTPSErrors: true,
    extraHTTPHeaders: { 'my-device-id': 'value' } });

const response = await pw.post(fsUrl, {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    form: {
                        UserName: usr.login,
                        Password: usr.password,
                        AuthMethod: 'FormsAuthentication',
                    },
                });

                console.log(response.headersArray());

Context: [email protected]

I tried to add request header 'Access-Control-Expose-Headers': 'Location',


Solution

  • In general, whenever location is provided as a response header by the server, the server wants the client to redirect to the location mentioned in the header.

    By default, the playwright does that redirection without waiting for anything and the headers you are getting are actually of the next request.

    To get the Location header to verify, you can stop the redirection as shown below:

    const pw = await request.newContext({
        ignoreHTTPSErrors: true,
        extraHTTPHeaders: { 'my-device-id': 'value' } 
    });
    
    const response = await pw.post(fsUrl, {
                        maxRedirects: 0,
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                        },
                        form: {
                            UserName: usr.login,
                            Password: usr.password,
                            AuthMethod: 'FormsAuthentication',
                        },
                    });
    
    console.log(response.headersArray());
    

    I have just added maxRedirects:0, to your request. Hope this helps!!