Skip to content

Commit fc735f5

Browse files
committed
Fixed issue with parsing the response string into a json object with having more than two dashes '--' in the returned data.
1 parent bd99662 commit fc735f5

File tree

9 files changed

+109
-32
lines changed

9 files changed

+109
-32
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-http-batcher",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "Enables transparent HTTP batch requests with Angular",
55
"main": "dist/angular-http-batch.min.js",
66
"keywords": [

dist/ChangeLog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
16/03/2015 V1.6.0
2+
Fixed issue with parsing the response string into a json object with having more than two dashes '--' in the returned data.
3+
14
19/11/2014 V1.5.0
25
When batching requests that have request bodies, such as "POST" requests, the standard angular request transforms convert objects into json strings.
36
When angular-http-batcher calls angular.toJson again, we end up with a single json string, instead of a json encoding of the object in the batched request.

dist/angular-http-batch.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* angular-http-batcher - v1.5.0 - 2014-11-19
2+
* angular-http-batcher - v1.6.0 - 2015-03-16
33
* https://github.com/jonsamwell/angular-http-batcher
4-
* Copyright (c) 2014 Jon Samwell
4+
* Copyright (c) 2015 Jon Samwell
55
*/
66
(function (window, angular) {
77
'use strict';
@@ -170,9 +170,10 @@ angular.module(window.ahb.name).factory('httpBatcher', [
170170

171171
currentBatchedRequests = {},
172172

173-
BatchRequestPartParser = function (part, request) {
173+
BatchRequestPartParser = function (part, request, boundaryToken) {
174174
this.part = part;
175175
this.request = request;
176+
this.boundaryToken = boundaryToken;
176177
},
177178

178179
BatchRequestManager = function (config, sendCallback) {
@@ -277,7 +278,7 @@ angular.module(window.ahb.name).factory('httpBatcher', [
277278
// need to get all the lines left apart from the last multipart seperator.
278279
result.data = '';
279280
j = 1;
280-
regex = new RegExp('--.*--', 'i');
281+
regex = new RegExp('--' + this.boundaryToken + '--', 'i');
281282
while (regex.test(responsePart) === false && ((i + j) <= responseParts.length)) {
282283
result.data += responsePart;
283284
responsePart = responseParts[i + j];
@@ -337,7 +338,6 @@ angular.module(window.ahb.name).factory('httpBatcher', [
337338
batchBody.push(constants.emptyString);
338339

339340
if (request.data) {
340-
//batchBody.push(angular.toJson(request.data));
341341
batchBody.push(request.data);
342342
}
343343

@@ -363,7 +363,7 @@ angular.module(window.ahb.name).factory('httpBatcher', [
363363
for (i = 0; i < parts.length; i += 1) {
364364
part = parts[i];
365365
if (part !== constants.emptyString) {
366-
responseParser = new BatchRequestPartParser(part, self.requests[responseCount]);
366+
responseParser = new BatchRequestPartParser(part, self.requests[responseCount], boundaryToken);
367367
responseParser.process();
368368
responseCount += 1;
369369
}

dist/angular-http-batch.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-http-batcher",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "Enables transparent HTTP batch requests with Angular",
55
"main": "angular-http-batcher.min.js",
66
"scripts": {
2.5 KB
Binary file not shown.

src/services/httpBatcher.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ angular.module(window.ahb.name).factory('httpBatcher', [
1818

1919
currentBatchedRequests = {},
2020

21-
BatchRequestPartParser = function (part, request) {
21+
BatchRequestPartParser = function (part, request, boundaryToken) {
2222
this.part = part;
2323
this.request = request;
24+
this.boundaryToken = boundaryToken;
2425
},
2526

2627
BatchRequestManager = function (config, sendCallback) {
@@ -125,7 +126,7 @@ angular.module(window.ahb.name).factory('httpBatcher', [
125126
// need to get all the lines left apart from the last multipart seperator.
126127
result.data = '';
127128
j = 1;
128-
regex = new RegExp('--.*--', 'i');
129+
regex = new RegExp('--' + this.boundaryToken + '--', 'i');
129130
while (regex.test(responsePart) === false && ((i + j) <= responseParts.length)) {
130131
result.data += responsePart;
131132
responsePart = responseParts[i + j];
@@ -185,7 +186,6 @@ angular.module(window.ahb.name).factory('httpBatcher', [
185186
batchBody.push(constants.emptyString);
186187

187188
if (request.data) {
188-
//batchBody.push(angular.toJson(request.data));
189189
batchBody.push(request.data);
190190
}
191191

@@ -211,7 +211,7 @@ angular.module(window.ahb.name).factory('httpBatcher', [
211211
for (i = 0; i < parts.length; i += 1) {
212212
part = parts[i];
213213
if (part !== constants.emptyString) {
214-
responseParser = new BatchRequestPartParser(part, self.requests[responseCount]);
214+
responseParser = new BatchRequestPartParser(part, self.requests[responseCount], boundaryToken);
215215
responseParser.process();
216216
responseCount += 1;
217217
}

tests/index.html

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,35 @@
3939
'$http',
4040
function ($scope, $http) {
4141
$scope.callSingle = function () {
42-
$http.get('http://fsatnav:8080/api/products', { cache: true }).then(function (data) {
43-
console.log('success 0 - ' + data.data);
42+
// $http.get('http://localhost:8080/api/products', { cache: true }).then(function (data) {
43+
// console.log('success 0 - ' + data.data);
44+
// }, function (err) {
45+
// console.log('error 0 - ' + err);
46+
// });
47+
48+
$http.get('http://localhost:8080/api/products/2').then(function (data) {
49+
console.log('success 1 - ' + data.data);
4450
}, function (err) {
45-
console.log('error 0 - ' + err);
51+
console.log('error 1 - ' + err);
4652
});
4753

48-
// $http.get('http://fsatnav:8080/api/products/2').then(function (data) {
49-
// console.log('success 1 - ' + data.data);
50-
// }, function (err) {
51-
// console.log('error 1 - ' + err);
52-
// });
53-
//
54-
// $http.put('http://fsatnav:8080/api/products', {
55-
// Name: 'Product X',
56-
// StockQuantity: 300
57-
// }).then(function (data) {
58-
// console.log('success 2 - ' + data.data);
59-
// }, function (err) {
60-
// console.log('error 2 - ' + angular.fromJson(err));
61-
// });
54+
var promise = $http.put('http://localhost:8080/api/products', {
55+
Name: 'Product X',
56+
StockQuantity: 300
57+
});
58+
59+
debugger;
60+
61+
promise.catch(function (data) {
62+
debugger;
63+
console.log('balls');
64+
});
65+
66+
promise.then(function (data) {
67+
console.log('success 2 - ' + data.data);
68+
}, function (err) {
69+
console.log('error 2 - ' + angular.fromJson(err));
70+
});
6271
};
6372
}]);
6473
</script>

0 commit comments

Comments
 (0)