1- import { Headers , Request , RequestMethod , RequestOptions , Response ,
2- ResponseOptions , ResponseType } from "@angular/http" ;
1+ import { Headers , Request , RequestMethod , RequestOptions ,
2+ Response , ResponseOptions , ResponseType } from "@angular/http" ;
33import { Observable } from "rxjs/Observable" ;
44import { HttpBatchConfiguration } from "../batch-configuration" ;
55import { IBatchHttpRequestAdapter } from "./batching-adapter" ;
66
7- const XSSI_PREFIX = / ^ \) \] \} ' , ? \n / ;
7+ const XSSI_PREFIX = / ^ \) \] \} " , ? \n / ;
88
99/**
1010 * See https://cloud.google.com/storage/docs/json_api/v1/how-tos/batch
@@ -42,7 +42,8 @@ export class HttpMultipartMixedBoundaryAdapter implements IBatchHttpRequestAdapt
4242 `Host: ${ urlParts . host } ` ,
4343 `Accept: application/json, text/plain, */*` ) ;
4444
45- // request's normal headers
45+ // request"s normal headers
46+ this . setDetectedContentType ( r ) ;
4647 r . headers . forEach ( ( values , name ) => {
4748 let header = `${ name } : ${ values . join ( "," ) } ` ;
4849 if ( this . configuration . uniqueRequestName !== undefined &&
@@ -61,8 +62,12 @@ export class HttpMultipartMixedBoundaryAdapter implements IBatchHttpRequestAdapt
6162 const body = r . getBody ( ) ; // returns null if no body :-(
6263 // tslint:disable-next-line:no-null-keyword
6364 if ( body !== null ) {
64- bodyParts . push ( body ) ;
65- bodyParts . push ( HttpMultipartMixedBoundaryAdapter . EMPTY_STRING ) ;
65+ if ( r . detectContentTypeFromBody ( ) === 1 ) {
66+ // r.getBody pretty prints JSON which causes deserialisation errors in web api
67+ bodyParts . push ( JSON . stringify ( JSON . parse ( body ) ) ) ;
68+ } else {
69+ bodyParts . push ( body ) ;
70+ }
6671 }
6772
6873 bodyParts . push ( HttpMultipartMixedBoundaryAdapter . EMPTY_STRING ) ;
@@ -143,7 +148,7 @@ export class HttpMultipartMixedBoundaryAdapter implements IBatchHttpRequestAdapt
143148 private getUrlParts ( url : string ) : { host : string ; path : string , search : string } {
144149 const anchorElement = document . createElement ( "a" ) ;
145150 anchorElement . href = url ;
146- // IE < 11 & Safari 11 seem to remove the proceeding '/' form urls
151+ // IE < 11 & Safari 11 seem to remove the proceeding "/" form urls
147152 // which causings routing errors in web api
148153 // https://localhost:44379/authentication/profile/current should be
149154 // path: "/authentication/profile/current"
@@ -159,4 +164,34 @@ export class HttpMultipartMixedBoundaryAdapter implements IBatchHttpRequestAdapt
159164 private ensureLeadingBackSlash ( path : string ) : string {
160165 return path [ 0 ] === "/" ? path : `/${ path } ` ;
161166 }
167+
168+ private setDetectedContentType ( req : Request ) {
169+ // Skip if a custom Content-Type header is provided
170+ if ( req . headers != null && req . headers . get ( "Content-Type" ) != null ) {
171+ return ;
172+ }
173+
174+ let contentType : string ;
175+
176+ // Set the detected content type
177+ switch ( req . detectContentType ( ) ) {
178+ case 1 :
179+ contentType = "application/json" ;
180+ break ;
181+ case 2 :
182+ contentType = "application/x-www-form-urlencoded;charset=UTF-8" ;
183+ break ;
184+ case 5 :
185+ const blob = req . blob ( ) ;
186+ if ( blob . type ) {
187+ contentType = blob . type ;
188+ }
189+ break ;
190+ default :
191+ contentType = "text/plain" ;
192+ break ;
193+ }
194+
195+ req . headers . append ( "Content-Type" , contentType ) ;
196+ }
162197}
0 commit comments