8000 fix(ajax): Fix case-insensitive headers in HTTP request (#4453) · ReactiveX/rxjs@673bf47 · GitHub
[go: up one dir, main page]

Skip to content

Commit 673bf47

Browse files
DostalTomasbenlesh
authored andcommitted
fix(ajax): Fix case-insensitive headers in HTTP request (#4453)
In RFC 7230 and RFC 7540 is written, that HTTP headers is case-insensitive, so this is fix for correct serializing request body base on HTTP headers.
1 parent be4f419 commit 673bf47

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

spec/observables/dom/ajax-spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,26 @@ describe('ajax', () => {
600600
expect(MockXMLHttpRequest.mostRecent.data).to.equal('{"🌟":"🚀"}');
601601
});
602602

603+
it('should send json body not mattered on case-sensitivity of HTTP headers', () => {
604+
const body = {
605+
hello: 'world'
606+
};
607+
608+
const requestObj = {
609+
url: '/flibbertyJibbet',
610+
method: '',
611+
body: body,
612+
headers: {
613+
'cOnTeNt-TyPe': 'application/json; charset=UTF-8'
614+
}
615+
};
616+
617+
ajax(requestObj).subscribe();
618+
619+
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
620+
expect(MockXMLHttpRequest.mostRecent.data).to.equal('{"hello":"world"}');
621+
});
622+
603623
it('should error if send request throws', (done: MochaDone) => {
604624
const expected = new Error('xhr send failure');
605625

src/internal/observable/dom/AjaxObservable.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,18 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
202202
const headers = request.headers = request.headers || {};
203203

204204
// force CORS if requested
205-
if (!request.crossDomain && !headers['X-Requested-With']) {
205+
if (!request.crossDomain && !this.getHeader(headers, 'X-Requested-With')) {
206206
headers['X-Requested-With'] = 'XMLHttpRequest';
207207
}
208208

209209
// ensure content type is set
210-
if (!('Content-Type' in headers) && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
210+
let contentTypeHeader = this.getHeader(headers, 'Content-Type');
211+
if (!contentTypeHeader && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
211212
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
212213
}
213214

214215
// properly serialize body
215-
request.body = this.serializeBody(request.body, request.headers['Content-Type']);
216+
request.body = this.serializeBody(request.body, this.getHeader(request.headers, 'Content-Type'));
216217

217218
this.send();
218219
}
@@ -315,6 +316,16 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
315316
}
316317
}
317318

319+
private getHeader(headers: {}, headerName: string): any {
320+
for (let key in headers) {
321+
if (key.toLowerCase() === headerName.toLowerCase()) {
322+
return headers[key];
323+
}
324+
}
325+
326+
return undefined;
327+
}
328+
318329
private setupEvents(xhr: XMLHttpRequest, request: AjaxRequest) {
319330
const progressSubscriber = request.progressSubscriber;
320331

0 commit comments

Comments
 (0)
0