Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode request data as JSON when Content-Type is set to application/json #118

Closed
jonkoops opened this issue Nov 4, 2013 · 13 comments
Closed

Comments

@jonkoops
Copy link

jonkoops commented Nov 4, 2013

When the the Content-Type header of a request is set to application/json shouldn't the request data be converted to a JSON string using JSON.stringify?

Currently I'm sending my requests like this:

reqwest({
    url: '/api/v1/user',
    type: 'json',
    method: 'post',
    contentType: 'application/json',
    processData: false,
    data: JSON.stringify({
        first_name: 'John',
        last_name: 'Doe'
    })
});

But it would be easier if I could do this:

reqwest({
    url: '/api/v1/user',
    type: 'json',
    method: 'post',
    contentType: 'application/json',
    data: {
        first_name: 'John',
        last_name: 'Doe'
    }
});
@ded
Copy link
Owner

ded commented Nov 5, 2013

you can just do this:

reqwest({
  url: '/api/v1/user',
  method: 'post',
  data: {
    first_name: 'Json',
    last_name: 'Doe'
  }
})

@ded
Copy link
Owner

ded commented Nov 5, 2013

but to answer your question, should it be converted? i don't know. is there an authoritative source saying that it should be converted? is that the standard practice?

@jonkoops
Copy link
Author

jonkoops commented Nov 8, 2013

@ded It's a common practice to detect the Content-Type header that is returned by the server and decode the content into an Object. jQuery does this with JSON and XML returned by the server. I see no reason why we couldn't serialize Objects into their respective content-types when they are sent to the server.

@apoco
Copy link

apoco commented Oct 17, 2014

@ded, what you posted does not work for me. Rather, a content type of application/x-www-form-urlencoded; charset=UTF-8 is sent.

I also am unable to just pass a POJO and have it converted to JSON.

@jhollingworth
Copy link

+1 very really annoying. it would be nice to support same behaviour as jquery

@ruifortes
Copy link

Same here +1

@Huxpro
Copy link

Huxpro commented Sep 25, 2015

+1

@jonkoops
Copy link
Author

I'd recommend using the new Fetch API, it does the same as request but it has a nice Promise based API. It's not in browsers yet but there are polyfills available.

@Huxpro
Copy link

Huxpro commented Sep 27, 2015

@jonkoops fully agree

@danhardman
Copy link

+1 Just started using reqwest and encountered this. Definitely would be a great addition.

@torarnek
Copy link

+1

1 similar comment
@mrconnormurphy
Copy link

+1

@danhardman
Copy link

Opened a pull request #238

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants