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.

setHeader Function not setting header

See original GitHub issue

Hey there @skellock ,

I am trying to override my default headers assigned at apisauce.create() for one specific request. I tried using the api.setHeader and api.setHeaders functions. I need to set the header to Content-Type multipart/form-data.

Ive included a screenshot of my request headers, after attempting to modify with setHeader.

var body = new FormData() body.append(‘file’, photo) api.setHeader({‘Content-Type’: ‘multipart/form-data’}) const response = api.put(/user/photo/${id}, body, {headers: {‘Content-Type’: ‘multipart/form-data’}}) return response

Thanks in advance for your help!

screen shot 2017-07-20 at 5 36 31 pm

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
jkeamcommented, Mar 12, 2019

I was unable to reproduce the bug. Here is my test (using v1.0.2):

// app.js
const apisauce = require('apisauce')
const http = require('http')

const PORT = '8080'

// start server
const server = http.createServer()
server.on('request', (request, response) => {
  console.log(request.headers)
}).listen(PORT)

// client with old header
const api = apisauce.create({
  baseURL: `http://localhost:${PORT}`,
  timeout: 30000,
  headers: { 'Authorization': 'BEARER OLD_TOKEN'}
})
api.get('/')

// client with new header
api.setHeader('Authorization', 'BEARER NEW_TOKEN')
api.get('/')

Console output:

> node ./app.js

{ accept: 'application/json',
  'content-type': 'application/json',
  authorization: 'BEARER OLD_TOKEN',
  'user-agent': 'axios/0.18.0',
  host: 'localhost:8080',
  connection: 'close' }
{ accept: 'application/json',
  'content-type': 'application/json',
  authorization: 'BEARER NEW_TOKEN',
  'user-agent': 'axios/0.18.0',
  host: 'localhost:8080',
  connection: 'close' }
1reaction
skellockcommented, Jul 21, 2017

Evening! I think you want setHeaders instead of setHeader here. You can use setHeader but the interface is 2 strings, not an object. That where that [object Object] is coming from.

i think you can remove that line entirely looking at your code tho.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Can't set headers after they are sent to the client
The res object in Express is a subclass of Node.js's http.ServerResponse (read the http.js source). You are allowed to call res.setHeader(name, value) as ......
Read more >
Node.js response.setHeader() Method - GeeksforGeeks
When headers have been set with response.setHeader(), they will be merged with any headers passed to response.writeHead(), with the headers ...
Read more >
Cannot set headers after they are sent to the client in JS
The "Cannot set headers after they are sent to the client" error occurs when the server in an express.js application sends more than...
Read more >
XMLHttpRequest.setRequestHeader() - Web APIs | MDN
The XMLHttpRequest method setRequestHeader() sets the value of an HTTP ... If no Accept header has been set using this, an Accept header...
Read more >
Data set header - IBM
It may or may not contain data set headers. If the SYSIN data set has record ... Output Processing Section -- for advanced...
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