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.

Sends Content-Type: json when there is no payload on Nodejs

See original GitHub issue

Summary

axios sends content-type: application/json when sending data as params (query string) and when no data at all for other http methods like DELETE

All api calls were of the form

axios({
    method: "delete"..
}

The fix was to manually set headers["content-type"]="" for such requests.

Context

This happens in node only and works fine in browser.

  • axios version: v0.18.0
  • Environment: node v8.10.0, windows 10

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chinesedfancommented, Mar 1, 2020

Closed due to too old. If you still have this problem, feel free to open a new one with detailed information. Thanks.

0reactions
fahimalizaincommented, Oct 3, 2018

If you look at the _header property of ClientRequest you could see that Content-Type: application/json is set.

ClientRequest {
        domain: null,
        _events: [Object],
        _eventsCount: 6,
        _maxListeners: undefined,
        output: [],
        outputEncodings: [],
        outputCallbacks: [],
        outputSize: 0,
        writable: true,
        _last: true,
        upgrading: false,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 0,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Object],
        connection: [Object],
        _header: 'DELETE /api/resource/Item/ITEM%20CD HTTP/1.1\r\nAccept: */*\r\nAccept: application/json\r\nCookie: __cfduid=d8e4a4b1cd47534f72f567dfc66200c2e1538560426;user_image=;user_id=Administrator;system_user=yes;full_name=Administrator;sid=362ba7e0aa0df024302e5962edc0efcec4769411e078f16ae51708d8;\r\nContent-Type: application/json\r\nUser-Agent: axios/0.18.0\r\nHost: test-erp.leam.ae\r\nConnection: close\r\n\r\n',        _onPendingData: [Function: noopPendingOutput],
        agent: [Object],
        socketPath: undefined,
        timeout: undefined,
        method: 'DELETE',
        path: '/api/resource/Item/ITEM%20CD',
        _ended: true,
        res: [Object],
        aborted: undefined,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: null,
        maxHeadersCount: null,
        _redirectable: [Object],
        [Symbol(outHeadersKey)]: [Object] }

Hi @faztp12 ,

According to this, the default Content-Type is application/x-www-form-urlencoded. Are you using a custom axios instance or just the default one?

axios/lib/defaults.js

Lines 6 to 8 in b4c5d35

var DEFAULT_CONTENT_TYPE = { ‘Content-Type’: ‘application/x-www-form-urlencoded’ }; Cheers 😄 ,

I am using the default one. Searching for the keyword in my entire project brings up only 3 occurrences:

import axios from "axios";
axios({
    ... options ...
})

Axios: 0.18.0 Node: 8.10 All fine in browser

Hope it helps 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

send Content-Type: application/json post with node.js
When sending JSON you just have to put it in body of the option. ... body: JSON.stringify(payload), headers: { "Content-Type": ...
Read more >
Anatomy of an HTTP Transaction - Node.js
To do this, there's a method called [ writeHead ][], which writes the status code and the headers to the stream. response.writeHead(200, {...
Read more >
Node-Red HTTP Request Node for Beginners
Common examples are setting the payload type when sending JSON data in post messages. You can use a function node or change node...
Read more >
Reply - Fastify
serialize(payload) - Serializes the specified payload using the default JSON serializer or using the custom serializer (if one is set) and returns the ......
Read more >
How do I send a POST request using JavaScript? - ReqBin
To send HTTP POST and GET requests in Node.js, use the built-in "http" module. In order to send the HTTP POST request using...
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