How can I inject a custom HTTP Header into every request that SuperAgent makes?

前端 未结 3 598
情话喂你
情话喂你 2021-02-07 10:16

Clearly SuperAgent supports custom HTTP headers:

request
   .post(\'/api/pet\')
   .send({ name: \'Manny\', species: \'cat\' })
   .set(\'X-API-Key\', \'foobar\'         


        
3条回答
  •  醉梦人生
    2021-02-07 10:36

    This could be the late answer but I have used superagent-use plugin to inject a custom HTTP header in all the requests. First of all, you need to install superagent-use

    npm install superagent-use --save
    

    then require like this

    // using plugin to intercept calls
    var request = require('superagent-use')(require('superagent'));
    

    then add the function as middlerware/interceptor

    // interceptor used by superagent to add custom header for each request
    request.use((req) => {
        req.header.custom_header = any_value;
        return req;
    });
    

    and finally

    request
        .get(url)
        .query({ view: 'jsonView' }) // query string
    

提交回复
热议问题