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

AJAX Content-Type Handling. #2837

Closed
Parakleta opened this issue Sep 17, 2017 · 3 comments · Fixed by #5661
Closed

AJAX Content-Type Handling. #2837

Parakleta opened this issue Sep 17, 2017 · 3 comments · Fixed by #5661
Assignees
Labels
bug Confirmed bug

Comments

@Parakleta
Copy link

When sending data using the AJAX methods the existing code attempts to serialise the data using its own rules which do not match the XHR serialisation rules. For my use notably it doesn't automatically handle XML Document objects which XHR does. I'm not sure why the decision was made to make 'application/x-www-form-urlencoded' the default content type, and while the serialisation code is helpful I feel it should be opt in rather than the default behaviour.

Additionally, when checking to see if the user has supplied a 'Content-Type' header to override the default behaviour it only does a case-sensitive check despite the fact that HTTP headers are case insensitive. It should instead either normalise user provided headers, or else do case insensitive checks for relevant headers, or else it should be clearly documented that it will interrogate specific headers in a case insensitive fashion. See:

if (!request.crossDomain && !headers['X-Requested-With']) {
headers['X-Requested-With'] = 'XMLHttpRequest';
}
// ensure content type is set
if (!('Content-Type' in headers) && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
}
// properly serialize body
request.body = this.serializeBody(request.body, request.headers['Content-Type']);

At the very least clearly documenting the default serialisation behaviour and how to use 'Content-Type' to override the default behaviour is required.

@benlesh benlesh added the help wanted Issues we wouldn't mind assistance with. label Mar 16, 2018
@WhoAteDaCake
Copy link

Is there a reason why we can't let browser detect it automatically ? That's what axios does at the moment.
At least when uploading FormData data of Content-Type multipart/form-data, browser would automatically write the type and boundaries.

@OliverJAsh
Copy link
Contributor

Fixed by #4453?

@Parakleta
Copy link
Author

Not really. I maybe shouldn't have combined two problems in a single issue, but the primary problem here is that I found the serialising behaviour unintuitive coming from XHR. The default type of application/x-www-form-urlencoded for everything except FormData seems an odd choice, doesn't match with expectations from the original XHR behaviour, and I couldn't find it documented anywhere.

There are default rules for how to type and serialise the request body in the case of XHR which could be replicated. I'm not aware of arguments against the default XHR behaviour, but for the lack of any better documentation it seems it would be advantageous for the RxJS AJAX to conform to 'legacy' behaviour.

That PR should at least make it straight-forward to override the default behaviour which is a decent improvement.

@benlesh benlesh self-assigned this Sep 2, 2020
@benlesh benlesh added bug Confirmed bug and removed help wanted Issues we wouldn't mind assistance with. labels Sep 2, 2020
benlesh added a commit to benlesh/rxjs that referenced this issue Sep 2, 2020
…pe where possible

- Ajax will now allow default behavior as defined in the specs:
  - https://xhr.spec.whatwg.org/#the-send()-method
  - https://fetch.spec.whatwg.org/#concept-bodyinit-extract
- if the `body` is set to: `Blob`, `ArrayBuffer`, any array buffer view (like a byte sequence, e.g. `Uint8Array`, etc), `FormData`, `URLSearchParams`, `string`, or `ReadableStream`, default handling is use. If the `body` is otherwise `typeof` `"object"`, then it will be converted to JSON via `JSON.stringify`, and the `Content-Type` header will be set to `application/json;charset=utf-8`. All other types will emit an error.
- Updates tests.
- Puts the behavior inline with axios, et al.

resolves ReactiveX#2837
benlesh added a commit to benlesh/rxjs that referenced this issue Sep 2, 2020
…pe where possible

- Ajax will now allow default behavior as defined in the specs:
  - https://xhr.spec.whatwg.org/#the-send()-method
  - https://fetch.spec.whatwg.org/#concept-bodyinit-extract
- if the `body` is set to: `Blob`, `ArrayBuffer`, any array buffer view (like a byte sequence, e.g. `Uint8Array`, etc), `FormData`, `URLSearchParams`, `string`, or `ReadableStream`, default handling is use. If the `body` is otherwise `typeof` `"object"`, then it will be converted to JSON via `JSON.stringify`, and the `Content-Type` header will be set to `application/json;charset=utf-8`. All other types will emit an error.
- Updates tests.
- Puts the behavior in-line with Axios, et al.

resolves ReactiveX#2837

BREAKING CHANGE: `ajax` body serialization will now use default XHR behavior in all cases. If the body is a `Blob`, `ArrayBuffer`, any array buffer view (like a byte sequence, e.g. `Uint8Array`, etc), `FormData`, `URLSearchParams`, `string`, or `ReadableStream`, default handling is use. If the `body` is otherwise `typeof` `"object"`, then it will be converted to JSON via `JSON.stringify`, and the `Content-Type` header will be set to `application/json;charset=utf-8`. All other types will emit an error.

BREAKING CHANGE: The `Content-Type` header passed to `ajax` configuration no longer has any effect on the serialization behavior of the AJAX request.
benlesh added a commit to benlesh/rxjs that referenced this issue Sep 3, 2020
…pe where possible

- Ajax will now allow default behavior as defined in the specs:
  - https://xhr.spec.whatwg.org/#the-send()-method
  - https://fetch.spec.whatwg.org/#concept-bodyinit-extract
- if the `body` is set to: `Blob`, `ArrayBuffer`, any array buffer view (like a byte sequence, e.g. `Uint8Array`, etc), `FormData`, `URLSearchParams`, `string`, or `ReadableStream`, default handling is use. If the `body` is otherwise `typeof` `"object"`, then it will be converted to JSON via `JSON.stringify`, and the `Content-Type` header will be set to `application/json;charset=utf-8`. All other types will emit an error.
- Updates tests.
- Puts the behavior in-line with Axios, et al.

resolves ReactiveX#2837

BREAKING CHANGE: `ajax` body serialization will now use default XHR behavior in all cases. If the body is a `Blob`, `ArrayBuffer`, any array buffer view (like a byte sequence, e.g. `Uint8Array`, etc), `FormData`, `URLSearchParams`, `string`, or `ReadableStream`, default handling is use. If the `body` is otherwise `typeof` `"object"`, then it will be converted to JSON via `JSON.stringify`, and the `Content-Type` header will be set to `application/json;charset=utf-8`. All other types will emit an error.

BREAKING CHANGE: The `Content-Type` header passed to `ajax` configuration no longer has any effect on the serialization behavior of the AJAX request.
benlesh added a commit to benlesh/rxjs that referenced this issue Sep 3, 2020
…pe where possible

- Ajax will now allow default behavior as defined in the specs:
  - https://xhr.spec.whatwg.org/#the-send()-method
  - https://fetch.spec.whatwg.org/#concept-bodyinit-extract
- if the `body` is set to: `Blob`, `ArrayBuffer`, any array buffer view (like a byte sequence, e.g. `Uint8Array`, etc), `FormData`, `URLSearchParams`, `string`, or `ReadableStream`, default handling is use. If the `body` is otherwise `typeof` `"object"`, then it will be converted to JSON via `JSON.stringify`, and the `Content-Type` header will be set to `application/json;charset=utf-8`. All other types will emit an error.
- Updates tests.
- Puts the behavior in-line with Axios, et al.

resolves ReactiveX#2837

BREAKING CHANGE: `ajax` body serialization will now use default XHR behavior in all cases. If the body is a `Blob`, `ArrayBuffer`, any array buffer view (like a byte sequence, e.g. `Uint8Array`, etc), `FormData`, `URLSearchParams`, `string`, or `ReadableStream`, default handling is use. If the `body` is otherwise `typeof` `"object"`, then it will be converted to JSON via `JSON.stringify`, and the `Content-Type` header will be set to `application/json;charset=utf-8`. All other types will emit an error.

BREAKING CHANGE: The `Content-Type` header passed to `ajax` configuration no longer has any effect on the serialization behavior of the AJAX request.
benlesh added a commit that referenced this issue Sep 3, 2020
…pe where possible

- Ajax will now allow default behavior as defined in the specs:
  - https://xhr.spec.whatwg.org/#the-send()-method
  - https://fetch.spec.whatwg.org/#concept-bodyinit-extract
- if the `body` is set to: `Blob`, `ArrayBuffer`, any array buffer view (like a byte sequence, e.g. `Uint8Array`, etc), `FormData`, `URLSearchParams`, `string`, or `ReadableStream`, default handling is use. If the `body` is otherwise `typeof` `"object"`, then it will be converted to JSON via `JSON.stringify`, and the `Content-Type` header will be set to `application/json;charset=utf-8`. All other types will emit an error.
- Updates tests.
- Puts the behavior in-line with Axios, et al.

resolves #2837

BREAKING CHANGE: `ajax` body serialization will now use default XHR behavior in all cases. If the body is a `Blob`, `ArrayBuffer`, any array buffer view (like a byte sequence, e.g. `Uint8Array`, etc), `FormData`, `URLSearchParams`, `string`, or `ReadableStream`, default handling is use. If the `body` is otherwise `typeof` `"object"`, then it will be converted to JSON via `JSON.stringify`, and the `Content-Type` header will be set to `application/json;charset=utf-8`. All other types will emit an error.

BREAKING CHANGE: The `Content-Type` header passed to `ajax` configuration no longer has any effect on the serialization behavior of the AJAX request.
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.

4 participants