Michal Bryxí Michal Bryxí - 1 year ago 88
reST (reStructuredText) Question

Ember-data RESTAdapter without JSON payload

In my Ember v2.7.0 app, I need to use backend endpoint which does not return JSON payload. But it behaves like REST endpoint, so I thought I would just use

to fetch the data and convert the payload via

Created this little Ember-twiddle, which just tries to fetch data with non-JSON payload. And it fails. As far as I can tell, it fails in
code, trying to extract JSON from the payload. So that my serializer does not have a chance to process the data.

This seems a bit odd, because I thought that Serializer is the layer which is responsible for munching the payload.

  1. Is it possible to use
    for querying non-JSON endpoint?

  2. If not, what is the easiest way to have REST-like behaviour on non-JSON endpoint?

Answer Source

What you'll need to do here is creating your own adapter that derives from DS.RESTRAdapter and then override its ajaxOptions-method. There you could change its dataType to text instead. I imagine that they separated this into its own method for your exact purpose since it doesn't do much else.

The Ember Guides have a page about customizing adapters that can get you started, based on the original code from the Ember repository it should probably be something like this.

import DS from 'ember-data';
import Ember from 'ember';

const {
} = Ember;

export default DS.RESTAdapter.extend({
   ajaxOptions(url, type, options) {
    var hash = options || {};
    hash.url = url;
    hash.type = type;
    hash.dataType = 'text';
    hash.context = this;

    if (hash.data && type !== 'GET') {
      hash.contentType = 'text/plain; charset=utf-8';

    var headers = get(this, 'headers');
    if (headers !== undefined) {
      hash.beforeSend = function (xhr) {
        Object.keys(headers).forEach((key) =>  xhr.setRequestHeader(key, headers[key]));

    return hash;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download