Skip to content

Commit

Permalink
Fixed issue with parsing the response string into a json object with …
Browse files Browse the repository at this point in the history
…having more than two dashes '--' in the returned data.
  • Loading branch information
jonsamwell committed Mar 16, 2015
1 parent bd99662 commit fc735f5
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-http-batcher",
"version": "1.5.0",
"version": "1.6.0",
"description": "Enables transparent HTTP batch requests with Angular",
"main": "dist/angular-http-batch.min.js",
"keywords": [
Expand Down
3 changes: 3 additions & 0 deletions dist/ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
16/03/2015 V1.6.0
Fixed issue with parsing the response string into a json object with having more than two dashes '--' in the returned data.

19/11/2014 V1.5.0
When batching requests that have request bodies, such as "POST" requests, the standard angular request transforms convert objects into json strings.
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.
Expand Down
12 changes: 6 additions & 6 deletions dist/angular-http-batch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* angular-http-batcher - v1.5.0 - 2014-11-19
* angular-http-batcher - v1.6.0 - 2015-03-16
* https://github.com/jonsamwell/angular-http-batcher
* Copyright (c) 2014 Jon Samwell
* Copyright (c) 2015 Jon Samwell
*/
(function (window, angular) {
'use strict';
Expand Down Expand Up @@ -170,9 +170,10 @@ angular.module(window.ahb.name).factory('httpBatcher', [

currentBatchedRequests = {},

BatchRequestPartParser = function (part, request) {
BatchRequestPartParser = function (part, request, boundaryToken) {
this.part = part;
this.request = request;
this.boundaryToken = boundaryToken;
},

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

if (request.data) {
//batchBody.push(angular.toJson(request.data));
batchBody.push(request.data);
}

Expand All @@ -363,7 +363,7 @@ angular.module(window.ahb.name).factory('httpBatcher', [
for (i = 0; i < parts.length; i += 1) {
part = parts[i];
if (part !== constants.emptyString) {
responseParser = new BatchRequestPartParser(part, self.requests[responseCount]);
responseParser = new BatchRequestPartParser(part, self.requests[responseCount], boundaryToken);
responseParser.process();
responseCount += 1;
}
Expand Down
6 changes: 3 additions & 3 deletions dist/angular-http-batch.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-http-batcher",
"version": "1.5.0",
"version": "1.6.0",
"description": "Enables transparent HTTP batch requests with Angular",
"main": "angular-http-batcher.min.js",
"scripts": {
Expand Down
Binary file modified servers/WebApi2/WebApiHttpBatchServer.v12.suo
Binary file not shown.
8 changes: 4 additions & 4 deletions src/services/httpBatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ angular.module(window.ahb.name).factory('httpBatcher', [

currentBatchedRequests = {},

BatchRequestPartParser = function (part, request) {
BatchRequestPartParser = function (part, request, boundaryToken) {
this.part = part;
this.request = request;
this.boundaryToken = boundaryToken;
},

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

if (request.data) {
//batchBody.push(angular.toJson(request.data));
batchBody.push(request.data);
}

Expand All @@ -211,7 +211,7 @@ angular.module(window.ahb.name).factory('httpBatcher', [
for (i = 0; i < parts.length; i += 1) {
part = parts[i];
if (part !== constants.emptyString) {
responseParser = new BatchRequestPartParser(part, self.requests[responseCount]);
responseParser = new BatchRequestPartParser(part, self.requests[responseCount], boundaryToken);
responseParser.process();
responseCount += 1;
}
Expand Down
43 changes: 26 additions & 17 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,35 @@
'$http',
function ($scope, $http) {
$scope.callSingle = function () {
$http.get('http://fsatnav:8080/api/products', { cache: true }).then(function (data) {
console.log('success 0 - ' + data.data);
// $http.get('http://localhost:8080/api/products', { cache: true }).then(function (data) {
// console.log('success 0 - ' + data.data);
// }, function (err) {
// console.log('error 0 - ' + err);
// });

$http.get('http://localhost:8080/api/products/2').then(function (data) {
console.log('success 1 - ' + data.data);
}, function (err) {
console.log('error 0 - ' + err);
console.log('error 1 - ' + err);
});

// $http.get('http://fsatnav:8080/api/products/2').then(function (data) {
// console.log('success 1 - ' + data.data);
// }, function (err) {
// console.log('error 1 - ' + err);
// });
//
// $http.put('http://fsatnav:8080/api/products', {
// Name: 'Product X',
// StockQuantity: 300
// }).then(function (data) {
// console.log('success 2 - ' + data.data);
// }, function (err) {
// console.log('error 2 - ' + angular.fromJson(err));
// });
var promise = $http.put('http://localhost:8080/api/products', {
Name: 'Product X',
StockQuantity: 300
});

debugger;

promise.catch(function (data) {
debugger;
console.log('balls');
});

promise.then(function (data) {
console.log('success 2 - ' + data.data);
}, function (err) {
console.log('error 2 - ' + angular.fromJson(err));
});
};
}]);
</script>
Expand Down
Loading

0 comments on commit fc735f5

Please sign in to comment.