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.

Set Authorization header to post request

See original GitHub issue
  1. Request:
request(app)
      .post('/api/categories')
      .send(category)
      .set({ Authorization: token })
      .end((err, res) => {
        const newCategory = res.body;

        expect(res.status).to.be.equal(200);
        expect(newCategory).to.be.equal(category);

        done();
      });

Error: Error: "value" required in setHeader("Authorization", value)

  1. Request:
request(app)
      .post('/api/categories')
      .send(category)
      .setHeader('Authorization', token)
      .end((err, res) => {
        const newCategory = res.body;

        expect(res.status).to.be.equal(200);
        expect(newCategory).to.be.equal(category);

        done();
      });

Error: TypeError: (0 , _supertest2.default)(...).post(...).send(...).setHeader is not a function

How set header to post request ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

399reactions
gilbertcommented, Feb 17, 2018

Note that you have to call .set() AFTER calling .post(), not before. For example:

request(app)
  .set('Authorization', 'abc123') // DOES NOT WORK
  .post('/api')

request(app)
  .post('/api')
  .set('Authorization', 'abc123') // Works.
84reactions
mikelaxcommented, Jan 29, 2017

You do want to use the .set function. You should be able to pass a single JSON object or two parameters for the key and value.

.set('Accept', 'application/json')
.set({ 'API-Key': 'foobar', Accept: 'application/json' })

In your first example I would say that there is a problem with the variable token.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authorization - HTTP - MDN Web Docs - Mozilla
The HTTP Authorization request header can be used to provide credentials that authenticate a user agent with a server, allowing access to a ......
Read more >
http post - how to send Authorization header? - Stack Overflow
Unfornately there is no Authorization header. How should I add it correctly? Whole request sent by my code looks as follow: OPTIONS /oauth/token ......
Read more >
POST JSON With Bearer Token Authorization Header - ReqBin
To post JSON with a Bearer Token Authorization header, you need to make an HTTP POST request, provide your Bearer Token with an ......
Read more >
Authorizing requests - Postman Learning Center
You can pass auth details along with any request you send in Postman. Auth data can be included in the header, body, or...
Read more >
Set the Authorization Header with Axios - Mastering JS
Setting the authorization header is a little different with post() , because the 2nd parameter to post() is the request body. You should...
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