Skip to content

Commit 1854b8b

Browse files
committed
fix(http-batch-adapter): fixed serialisation issue with asp web api when json is formatted
1 parent eebf1fb commit 1854b8b

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
1.09
2+
========
3+
Fixed deserialisation issue with asp web api when json content is formatted in batch request
4+
5+
16
1.08
27
========
38
Fixed issue in IE11 that was stripping leading '/' on urls which in turned caused the routing in web api to fail.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-http-batcher",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "Angular (2+) HTTP batching module to reduce the number of HTTP requests and increase performance",
55
"scripts": {
66
"build": "./node_modules/.bin/ngc -p ./src",

src/adapters/http-multipart-mixed-boundary-adapter.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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";
33
import { Observable } from "rxjs/Observable";
44
import { HttpBatchConfiguration } from "../batch-configuration";
55
import { 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

Comments
 (0)