Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observable Ajax POST with wrong Content-Type after PUT #2801

Closed
nelsondaza opened this issue Aug 20, 2017 · 1 comment · Fixed by #5661
Closed

Observable Ajax POST with wrong Content-Type after PUT #2801

nelsondaza opened this issue Aug 20, 2017 · 1 comment · Fixed by #5661
Assignees
Labels
bug Confirmed bug

Comments

@nelsondaza
Copy link

nelsondaza commented Aug 20, 2017

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.

@atahrijouti
Copy link

@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

@benlesh benlesh added the bug Confirmed bug label Aug 20, 2020
@benlesh benlesh self-assigned this Aug 20, 2020
benlesh added a commit to benlesh/rxjs that referenced this issue Aug 20, 2020
- Also ensures x-requested-with header is set properly
- Adds comments
- Code clean up
- Normalize headers to be lowercase, since they are case insensitive.
- Updates tests
- Remove superfluous checks in unsubscribe

resolves ReactiveX#2801
benlesh added a commit to benlesh/rxjs that referenced this issue Sep 3, 2020
- Also ensures x-requested-with header is set properly
- Adds comments
- Code clean up
- Normalize headers to be lowercase, since they are case insensitive.
- Updates tests
- Remove superfluous checks in unsubscribe

resolves ReactiveX#2801
benlesh added a commit to benlesh/rxjs that referenced this issue Sep 3, 2020
- Also ensures x-requested-with header is set properly
- Adds comments
- Code clean up
- Normalize headers to be lowercase, since they are case insensitive.
- Updates tests
- Remove superfluous checks in unsubscribe

resolves ReactiveX#2801
benlesh added a commit that referenced this issue Sep 3, 2020
- Also ensures x-requested-with header is set properly
- Adds comments
- Code clean up
- Normalize headers to be lowercase, since they are case insensitive.
- Updates tests
- Remove superfluous checks in unsubscribe

resolves #2801
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants