jonsamwell / angular-http-batcher

Enables HTTP batch request with AngularJS
MIT License
96 stars 28 forks source link

Syntax Error parsing content #3

Closed goleafs closed 9 years ago

goleafs commented 9 years ago

Great little utility module - thanks for sharing it!

I am running into some issues handling the content returned.

For example, when an array is returned, it ends up trying to convert "[" to a type and dies with syntax error.

the exception occurs in convertDataToCorrectType, called from line 276: result.data = convertDataToCorrectType(result.contentType, responsePart);

here is a sample of the returned content in this case;

--79effe20-dcca-43e8-922b-34f7df66446e
Content-Type: application/http; msgtype=response

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

[
  {
    "caption": "A",
    "items": [
      {
        "a": "b",
        "c": "d"
      },
      {
        "a": "b",
        "c": "d"
      }
    ]
  },
  {
    "caption": "B",
    "items": [
      {
        "a": "b",
        "c": "d"
      },
      {
        "a": "b",
        "c": "d"
      }
    ]
  }
]

It looks like it is expected that all the data is on 1 line but I am running this in debug mode. I tried to work around this by altering your code a bit

e.g.

                        } else if (result.data === undefined && parsedSpaceBetweenHeadersAndMessage) {
                            var joinedData = responseParts.slice(i, responseParts.length).join('');
                            result.data = convertDataToCorrectType(result.contentType, joinedData );
                            break;
                        }

this works for most cases but the last part will include the multi-part separator so convertDataToCorrectType will fail. I can keep working around the formatting and handle that also but this is starting to get hacky so I thought I'd check to see if there is a better way to handle this or if anyone else has encountered this issue.

jonsamwell commented 9 years ago

Hi,

Thanks for using the module - I should be able to take a look at this this week :-)

Feel free to create a PR (will passing unit tests)

jonsamwell commented 9 years ago

I think I have fixed this in V1.4.0 (also on bower). Can you give it a test?

Thanks!