0

I’m using dojo/store/JsonRest to fetch for some data. But I also need the response headers. How can I access them?

this.transport = new JsonRest({
  target: "my/target"
});

this.transport.query({}).then(function(resp) {
  debugger; // <- I want to get the response headers here!
})

I tried accessing it by using the this keyword within the function at debugger time. But that is just the window object.

Is that even possible?

Castro Roy
  • 7,623
  • 13
  • 63
  • 97
chitzui
  • 3,778
  • 4
  • 28
  • 38
  • 1
    You are getting the window object in `this` because there is no context of the `.then` function. If you want to get the `this.transport` as `this` in the `.then` function then use `lang.hitch` -> `this.transport.query({}).then(lang.hitch(this.transport, function(resp) { //here this is this.transport} );` On another note, to get the response headers, you would have to either dig in the code of JsonRest or send the XHR using the `dojo.xhr` module. – Himanshu Jul 19 '18 at 03:43
  • use `.bind()` to set the scope of you function – GibboK Jul 19 '18 at 15:05

1 Answers1

1

dojo/store/JsonRest by itself do not provide a way to get the headers, but here is an example of how you can get all or individual headers.

var transport = new JsonRest({
  target: "my/target"
});

var result = transport.query({});

result.then(function(resp) {
  var localXHR = result.ioArgs.xhr;

  // get all headers, return an String
  console.log(localXHR.getAllResponseHeaders());

  // get one header
  console.log(localXHR.getResponseHeader('content-type'));

  // do something with the response
  console.log(resp);
});

Hope it helps

Castro Roy
  • 7,623
  • 13
  • 63
  • 97