paulvanbladel / aurelia-auth

:key: Authentication plugin for aurelia
200 stars 74 forks source link

Support aurelia-http-client in addition to fetch client #170

Open don-bluelinegrid opened 7 years ago

don-bluelinegrid commented 7 years ago

In our project we have a use case for file upload using multipart-form data, and this simply will not work using the aurelia Fetch client. I won't go into a detailed explanation, but Fetch will not work correctly with borowsers to send mulipart-form data, because it doesn't set the content-type and boundary value in the request. As a result, this makes it necessary for us to use the aurelia-http-client for this specific call for file upload.

The aurelia-authentication package provides a FetchConfig class for automatically configuring the aurelia-fetch-client with the appropriate token interceptor. We need similar support for the aurelia-http-client, so that the token interceptor can be attached and the auth token injected.

How can we get this support in http-client?

Thanks, Don

paulvanbladel commented 7 years ago

Hi Don, Yeah sorry about that, I was indeed a bit too enthousiast about Fetch when completetly replacing aurelia-http client :( Just wondering, isn't Fetch working correctly when you set content-type and boundary value? Or is Fetch simply unable to work with multi-part form data? Warm regards paul.

don-bluelinegrid commented 7 years ago

Paul -

Regarding your question:

  1. Technically, you are not supposed to set the content-type on a FetchPOST with FormData, because it is supposed to send it using the correct format. However, this is not really the case.

  2. It is possible to set the content-type, but this won't be a solution, because for multipart form data we need a boundary value, which is dynamically generated and set by the browser, to correspond with the form data parts. So, the content type value should look something like this: Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryqSQQKo8Bw6dtofBP. We can't manually do this.

  3. In general, it seems that no, Fetch is not really up to the task of working properly with multipart form data. This is a pretty widely held conclusion, if you search.

So, what would it take to get a similar level of support for http-client? Meaning, just adding the token interceptor to http-client, so that the JWT token is auto-injected in the request made by http-client?

Thanks, Don

don-bluelinegrid commented 7 years ago

I guess someone else was asking for this also, back in September:

https://github.com/paulvanbladel/aurelia-auth/issues/154

ClaudioNunes commented 7 years ago

Also fetch client in current implementations does not deal with timeouts. It would be nice that plugin also with aurelia-http-client that does deal with http timeouts.

PeterWone commented 7 years ago

I can't think of a single use case in which fetch is functionally superior to XHR. Unfortunately I can think of numerous cases in which it is less functional.