首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理/解析/读取“多部分/混合;boundary=batch”响应

如何处理/解析/读取“多部分/混合;boundary=batch”响应
EN

Stack Overflow用户
提问于 2014-01-20 08:39:48
回答 2查看 7.8K关注 0票数 11

如何使用JavaScript/jQuery处理/解析/读取类型为"multipart/mixed;boundary=batch“的响应?

在我们的应用程序中,我们得到了如下响应:

有办法处理这种反应吗?还是应该使用使用regex等的原始字符串操作来获取我们想要的内容?

代码语言:javascript
复制
--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--
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-14 19:43:59

不幸的是,似乎没有一个库来处理这个问题。这就是我最后要做的。下面的解决方案假设是有角度的,可以使用lodash ("_"),但是可以根据其他框架进行调整。

考虑到responseCollection是初始帖子中显示的http响应,我们首先从初始标头中找到边界。然后,使用该边界将响应拆分为其组件。在每个组件中,假设"{“的第一个实例标志着JSON的开始,而"}”的最后一个实例是结束。JSON被反序列化并推送到响应对象的集合上。

显然,这并不能适用于每一种场景,并且会做出一些宽泛的假设,但这足以解决我的问题。

代码语言:javascript
复制
    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;
    }
票数 8
EN

Stack Overflow用户

发布于 2016-09-17 05:56:10

代码语言:javascript
复制
var boundary= '--batch_';
var items = [];
var responseLines = batchapiresponse.split(boundary);
$.each(responseLines,function(index, value){

    var startJson = value.indexOf('{');
    var endJson = value.lastIndexOf('}');
    if (startJson < 0 || endJson < 0) 
    {
      return;               
    }
    var responseJson = value.substr(startJson, (endJson - startJson) + 1);
    responseJson=JSON.parse(responseJson);  
    items.push(responseJson);

});

注意:这是从jQuery的@SouthShoreAK之前的代码示例中派生出来的。

它的编写假设是,每个批处理响应都包含“-批处理_”作为边界中的子字符串。P.S主要为Google批处理请求编写

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21229418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档