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.

Send "Content-Type": "application/json; charset=utf-8" by default

See original GitHub issue

Hey folks, first of all great job with the library 😃

So I notice that ember-ajax sends data with a content type of application/x-www-form-urlencoded by default. Since sending JSON is so common, would you think it’s a good idea for application/json to be the default content type instead? (Like in superagent.)

For example, instead of this:

this.get('ajax').post(url, {
  contentType: 'application/json; charset=utf-8',
  data: JSON.stringify({
    hello: 'world',
    foo: 'bar'
  })
}

You could do something like this:

this.get('ajax').post(url, {
  data: {
    hello: 'world',
    foo: 'bar'
  }
})

I’m thinking it would just cut down on some boilerplate. What do you think?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:9
  • Comments:8

github_iconTop GitHub Comments

3reactions
nucleartidecommented, Jun 6, 2016

Yep, just tried it out - looks great! @cqcsobreyra’s idea is good, here’s the custom AjaxService I made to support my original use case:

import AjaxService from 'ember-ajax/services/ajax'

export default AjaxService.extend({
  contentType: 'application/json; charset=utf-8',

  options() {
    const result = this._super(...arguments)
    if (result.contentType === this.get('contentType')) {
      result.data = JSON.stringify(result.data)
    }
    return result
  }
})

Usage:

// import stuff
import Ember from 'ember'
const {
  inject: { service }
} = Ember

// inject service
ajax: service('custom-ajax')

// use
this.get('ajax').post('/helloworld', {
  data: {
    hello: 'world'
  }
})
1reaction
ChavaSobreyracommented, May 2, 2016

Just wanted to share an idea for a quick dirty fix how to achieve default options for those who might find it useful. In the ajax service you could do something like

import Ember from 'ember';
import AjaxService from 'ember-ajax/services/ajax';

export default AjaxService.extend({
    configuredRequest(url, options){
        var defaultOptions = {
            cache: false,
            contentType: 'application/json; charset=UTF-8',
            processData: false
        }

        if (options) {
            $.extend(defaultOptions, options);
        }
        this.request(url, options);
    }
});

And then just call this.get(‘ajax’).configuredRequest(‘/yoururl’, yourOptions);

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does "Content-type: application/json; charset=utf-8 ...
Content -type: application/json; charset=utf-8 designates the content to be in JSON format, encoded in the UTF-8 character encoding. Designating the encoding is ...
Read more >
encoding for Content-Type application/json should be UTF-8 ...
The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627).
Read more >
Content-Type - HTTP - MDN Web Docs - Mozilla
In requests, (such as POST or PUT ), the client tells the server what type of data ... Content-Type: text/html; charset=utf-8 Content-Type: ...
Read more >
Make default charset UTF-8 when using `receiveText` for ...
Request with Content-Type: application/json is decoded not as UTF-8, while request with Content-Type: application/json; charset=utf-8 is decoded correctly.
Read more >
What does “Content-type: application/json; charset=utf-8 ...
Content -type: application/json; charset=utf-8 designates the content to be in JSON format, encoded in the UTF-8 character encoding. Designating the encoding is ...
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