Vijey Vijey - 2 months ago 39
HTTP Question

how to process/parse/read a "multipart/mixed; boundary=batch" response

How to process/parse/read a response that is of type "multipart/mixed; boundary=batch" using JavaScript/jQuery?

In our application we get a response as given below:

Is there a way to process these kind of responses? Or should we use raw string manipulations using regex, etc to get the content we want?

--batchresponse_e3e3tc10-1181-4b94-bb8a-952452769d53
Content-Type: multipart/mixed; boundary=changesetresponse_4sdflwerf-40ef-4347-8c77-b364e5d2e678

--changesetresponse_4sdflwerf-40ef-4347-8c77-b364e5d2e678
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 201 Created
DataServiceVersion: 1.0;
Content-Type: application/json;odata=verbose;charset=utf-8
Content-ID: 1
X-Content-Type-Options: nosniff
Cache-Control: no-cache
Location: <url1>

{"Some": "JSON response"}
--changesetresponse_4sdflwerf-40ef-4347-8c77-b364e5d2e678
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 204 No Content
Content-ID: 2
X-Content-Type-Options: nosniff
Cache-Control: no-cache
DataServiceVersion: 1.0;


--changesetresponse_4sdflwerf-40ef-4347-8c77-b364e5d2e678
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 204 No Content
Content-ID: 3
X-Content-Type-Options: nosniff
Cache-Control: no-cache
DataServiceVersion: 1.0;


--changesetresponse_4sdflwerf-40ef-4347-8c77-b364e5d2e678--
--batchresponse_e3e3tc10-1181-4b94-bb8a-952452769d53--

Answer

Unfortunately, there doesn't seem to be a library out there to handle this. Here's what I ended up doing. The following solution assumes angular and lodash ("_") are available, but it could be adapted to other frameworks.

Given that responseCollection is the http response shown in the initial post, we first find the boundary from the initial header. Then, use that boundary to split the response into its components. In each component, it is assumed that the first instance of "{" marks the beginning of JSON, and the last instance of "}" is the end. The JSON is deserialized and pushed onto the collection of response objects.

This obviously won't work for every scenario and makes some broad assumptions, but it was enough to solve my problem.

    function parseBatch(responseCollection) {
        var items = [];

        var boundary = getBatchSeparator(responseCollection);

        var responseLines = responseCollection.data.split('--' + boundary);

        _.forEach(responseLines, function (response) {
            var startJson = response.indexOf('{');
            var endJson = response.lastIndexOf('}');

            if (startJson < 0 || endJson < 0) {
                return;
            }

            var responseJson = response.substr(startJson, (endJson - startJson) + 1);

            var item = angular.fromJson(responseJson);

            items.push(item);
        });

        return items;
    }

    function getBatchSeparator(response) {
        var headers = response.headers();

        if (!headers['content-type'])
            return ''; //would probably be bad if this happens, but not sure it ever will.

        var components = headers['content-type'].split(';');

        var boundary = _.find(components, function (o) { return _.startsWith(_.trim(o), 'boundary=') });

        boundary = _.replace(boundary, 'boundary=', '');

        boundary = _.trim(boundary, '; ');

        return boundary;
    }