Skip to content

Content-Type application/x-www-form-urlencoded #362

Closed
@alborozd

Description

@alborozd

Try to send request with content type application/x-www-form-urlencoded

var data = Querystring.stringify({ 
                "grant_type": "password",
                "username": login,
                "password": password
            });
axios.post(Urls.login, data);

but there is no such header in request.

I tried to use code:

  var data = Querystring.stringify({ 
                "grant_type": "password",
                "username": login,
                "password": password
            });
        return axios({
            method: 'post',
            url: Urls.login,
            data: data,
            headers: {
                'Content-type': 'application/x-www-form-urlencoded'
            }
        });

same problem

jquery code works fine:

$.ajax({
            url: Urls.login,
            data: data,
            type: "POST",
            headers: {
                'Content-type': 'application/x-www-form-urlencoded'
            }
        });

How can I use axios to send request with this header?

Activity

mzabriskie

mzabriskie commented on Jun 28, 2016

@mzabriskie
Member

That should work as you've shown. Sounds like a bug. I will look into it.

alborozd

alborozd commented on Jun 29, 2016

@alborozd
Author

The cause is interceptors. I can't send request with that Content-Type only when I use interceptors.

Here is my code:

axios.interceptors.request.use(function (config) {
        var token = LoginStore.getJwt();
        if (token) {
            config.headers["Authorization"] = "Bearer " + token;
        }

            return config;
        }, function (error) {    
            return Promise.reject(error);
        });

Inside interceptor I can see 'Content-Type' header, but it is not sent to the server.
Do I correctly use interceptors?

rahuljiresal

rahuljiresal commented on Jun 30, 2016

@rahuljiresal

Any updates on this? I have the same issue. Axios doesn't send the header I set.

rahuljiresal

rahuljiresal commented on Jun 30, 2016

@rahuljiresal

This seems to be the culprit line → https://github.com/mzabriskie/axios/blob/master/lib/adapters/xhr.js#L117

Any idea why the Content-Type header is removed before the request is sent?

A workaround option is to change your data to data || {} when you're making an axios request. This will make sure data is not undefined.

mzabriskie

mzabriskie commented on Jun 30, 2016

@mzabriskie
Member

It looks like the logic for removing Content-Type when requestData is undefined came in this commit 9096d34. There is no indication why it was added however.

I would vote if the requestData is undefined and method is PUT, POST or PATCH and no Content-Type has been set, then default Content-Type to application/x-www-form-urlencoded. Otherwise just honor whatever has been specified.

nickuraltsev

nickuraltsev commented on Jun 30, 2016

@nickuraltsev
Contributor

@mzabriskie But in the code snippet provided by @alborozd, the data is set to Querystring.stringify({...}), so requestData should not be undefined, right?

alborozd

alborozd commented on Jul 1, 2016

@alborozd
Author

@mzabriskie I think you're right. When I use request interceptor fiddler shows that data is empty. Without interceptor I can see data and header and it works fine.

So, probably the problem occurs when you work with interceptors.

damianprzygodzki

damianprzygodzki commented on Jul 16, 2016

@damianprzygodzki

No interceptor is needed to crash this thing. I've set content type header defaults axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';, and i can't send any payload in POST body.

I've used workaround with URLSearchParams:

    var params = new URLSearchParams();
    params.append('email', email);
    return (dispatch) => {
        axios.post('/stack/' + uri, params)

But this is not supported by Safari and IE.

Edit: Okay :) Next update on workaround. Full supported workaround is send data as query string.
data: "flashcard=" + JSON.stringify(flashcard) + "&stackUri=" + stackUri. It hurts, but it works 👍

nickuraltsev

nickuraltsev commented on Jul 25, 2016

@nickuraltsev
Contributor

You can use a library like qs instead:

var qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 });
alborozd

alborozd commented on Jul 25, 2016

@alborozd
Author

this is not a solution. What is difference if I will use "Querystring" or "qs" ? The problem is the header ['Content-Type'] = 'application/x-www-form-urlencoded' is empty because of interceptors.

damianprzygodzki

damianprzygodzki commented on Dec 8, 2016

@damianprzygodzki

Any updates here? Because i've lost 1h today on researching why my POSTs are not affecting API (til i reminded about that issue)... Or there are no plans to fix that and it's better to go somewhere else?

Dunkat

Dunkat commented on Dec 8, 2016

@Dunkat

I have the same problem... still waiting for fix...

cadavre

cadavre commented on Dec 8, 2016

@cadavre

Please reopen @nickuraltsev as this is not fixed by your solution.

+1 for issue.

229 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kdeloach@radupotop@airs0urce@mzabriskie@rahuljiresal

        Issue actions

          Content-Type application/x-www-form-urlencoded · Issue #362 · axios/axios