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

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎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+
constbody={
605+
hello: 'world'
606+
};
607+
608+
constrequestObj={
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
constexpected=newError('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
constheaders=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'inheaders)&&!(root.FormData&&request.bodyinstanceofroot.FormData)&&typeofrequest.body!=='undefined'){
210+
letcontentTypeHeader=this.getHeader(headers,'Content-Type');
211+
if(!contentTypeHeader&&!(root.FormData&&request.bodyinstanceofroot.FormData)&&typeofrequest.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+
privategetHeader(headers: {},headerName: string): any{
320+
for(letkeyinheaders){
321+
if(key.toLowerCase()===headerName.toLowerCase()){
322+
returnheaders[key];
323+
}
324+
}
325+
326+
returnundefined;
327+
}
328+
318329
privatesetupEvents(xhr: XMLHttpRequest,request: AjaxRequest){
319330
constprogressSubscriber=request.progressSubscriber;
320331

0 commit comments

Comments
 (0)