Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alter the headers of a Request?

Is it possible to alter the headers of the Request object that is received by the fetch event?

Two attempts:

  1. Modify existing headers:

    self.addEventListener('fetch', function (event) {
      event.request.headers.set("foo", "bar");
      event.respondWith(fetch(event.request));
    });
    

    Fails with Failed to execute 'set' on 'Headers': Headers are immutable.

  2. Create new Request object:

    self.addEventListener('fetch', function (event) {
      var req = new Request(event.request, {
        headers: { "foo": "bar" }
      });
      event.respondWith(fetch(req));
    });
    

    Fails with Failed to construct 'Request': Cannot construct a Request with a Request whose mode is 'navigate' and a non-empty RequestInit.

(See also How to alter the headers of a Response?)

like image 896
mjs Avatar asked Feb 15 '16 23:02

mjs


People also ask

Can request headers be modified?

You can manipulate the headers of incoming HTTP requests through HTTP Request Header Modification Rules. Through these rules you can: Set the value of an HTTP request header to a literal string value, overwriting its previous value or adding a new header to the request.

How do I change HTTP response headers?

In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name. In the Value box, type the custom HTTP header value.

How do I create a custom request header?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

When to change the HTTP request headers?

Here are some of the scenarios where you might need to change the HTTP Request Headers: Testing the control and/or testing the different variants by establishing appropriate HTTP headers. The need to test the cases when different aspects of the web application or even the server logic have to be thoroughly tested.

How do I add a request header to an API check?

When building the steps for an API Check, we can click + Add a Request Header to supply one or more headers at each request step. In the example above, we’re using Splunk Synthetic Monitoring’s API Check to:

What is the behavior of the request if the headers passed?

The behavior of the request will not change as per the custom headers passed. The response headers look like below when you check the URL in the browser developer tool, network tab − To get the details of the headers from the requests module use.

What are HTTP headers and why are they important?

What are HTTP Headers HTTP headers are an important part of the HTTP protocol. They define an HTTP message (request or response) and allow the client and server to exchange optional metadata with the message. They are composed of a case-insensitive header field name followed by a colon, then a header field value.


2 Answers

Creating a new request object works as long as you set all the options:

// request is event.request sent by browser here 
var req = new Request(request.url, {
    method: request.method,
    headers: request.headers,
    mode: 'same-origin', // need to set this properly
    credentials: request.credentials,
    redirect: 'manual'   // let browser handle redirects
});

You cannot use the original mode if it is navigate (that's why you were getting an exception) and you probably want to pass redirection back to browser to let it change its URL instead of letting fetch handle it.

Make sure you don't set body on GET requests - fetch does not like it, but browsers sometimes generate GET requests with the body when responding to redirects from POST requests. fetch does not like it.

like image 88
pirxpilot Avatar answered Oct 05 '22 18:10

pirxpilot


You can create a new request based on the original one and override the headers:

new Request(originalRequest, {
  headers: {
    ...originalRequest.headers,
    foo: 'bar'
  }
})

See also: https://developer.mozilla.org/en-US/docs/Web/API/Request/Request

like image 41
caisah Avatar answered Oct 05 '22 20:10

caisah