cujojs / rest

RESTful HTTP client for JavaScript
http://cujojs.com/
Other
1k stars 146 forks source link

returned request.method is empty in interceptor #174

Closed Doogiemuc closed 5 years ago

Doogiemuc commented 7 years ago

More a question than an issue: I am writing a custom interceptor. Why is request.method empty during the request method of the interceptor.

export default interceptor({
  request: function (request, config, meta) {
    console.log(request.method);   // <=  emtpy here.  Will be filled later???
  }
});

I'd expect GET or POST?

Doogiemuc commented 6 years ago

A small workaround at least to distinguish between GET and other HTTP verbs is: if (request.entity) { console.log("This is *not* a GET request, cause it has a body"); }

scothis commented 6 years ago

rest.js provides default values as late as possible. For the request method, the root client will provide a default. For most clients, like xhr and node, the default is a GET unless there is an entity in which case the default is a POST. This doesn't hold true for clients that don't support those methods, like the jsonp client which is fundamentally restricted to GET.

The request method will be set on the request during the response phase.

export default interceptor({
  response: function (response, config, meta) {
    console.log(response.request.method);   // should be set (unless there was no actual request)
  }
});
scothis commented 5 years ago

rest.js is no longer maintained