Rxjs: Observable Ajax POST with wrong Content-Type after PUT

Created on 20 Aug 2017  路  1Comment  路  Source: ReactiveX/rxjs

I'm trying to upload an image but I found that, every now and then, the server says that the binary image was not sent.
After some tests I found that the POST calls stops working after a PUT call and that is because the "Content-Type" of the PUT remains in the Request Headers. Those calls are not mapped, chained or consecutive, simply different user actions.

I've made some changes to AjaxObservable.ts but don't know if it is the right solution.

RxJS version:
5.4.3

Code to reproduce:
The POST code goes something like this:

...
const formData = new FormData();
formData.append('picture', action.payload.image);
return ajax.post(
  `${URL_API}/action/picture`,
  formData,
  AUTH_HEADERS,
)
...

The PUT code goes something like this:

...
const newSettings = {
  ...
};
return ajax.put(
  `${URL_API}/action/info`,
  newSettings,
  AUTH_HEADERS,
)
...

Expected behavior:
I expected the Content-Type header to change according to the method, like.

1st call POST -> Content-Type: multipart/form-data; boundary=... (has an attachment)
2nd call PUT  -> Content-Type: application/x-www-form-urlencoded; charset=UTF-8
3rd call POST -> Content-Type: multipart/form-data; boundary=... (has an attachment)

Actual behavior:
The 3rd call has the binary data but the wrong Content-Type so the server won't get it.

1st call POST -> Content-Type: multipart/form-data; boundary=... (has an attachment)
2nd call PUT  -> Content-Type: application/x-www-form-urlencoded; charset=UTF-8
3rd call POST -> Content-Type: application/x-www-form-urlencoded; charset=UTF-8 (has an attachment)

My actual solution:
I just found that Rx is mutating the HEADERS object passed as parameter adding a Content-Type attribute that is used in the next requests.

Passing a new object each time will do the trick.

return ajax.post(
        `${URL_API}/user/picture`,
        formData,
        { ...AUTH_HEADERS },
      )

The issue was solved, maybe this helps some one or you can guide me to the right solution.

bug

Most helpful comment

@nelsondaza Thanks a lot !! if you'd hadn't mentioned that it was mutating the headers object I would've stayed on this bug till the ends of time !

+1 this needs to be fixed

>All comments

@nelsondaza Thanks a lot !! if you'd hadn't mentioned that it was mutating the headers object I would've stayed on this bug till the ends of time !

+1 this needs to be fixed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LittleFox94 picture LittleFox94  路  3Comments

benlesh picture benlesh  路  3Comments

benlesh picture benlesh  路  3Comments

Agraphie picture Agraphie  路  3Comments

marcusradell picture marcusradell  路  4Comments