javascript - JS Fetching batch data with HTTP - Stack Overflow

My RESTful service allows batching requests. I'm trying to bine requests into one batch with help

My RESTful service allows batching requests.

I'm trying to bine requests into one batch with help of Fetch API:

let req1 = {
        url: "/cups/count",
        options: {
           method: 'GET',
           headers: {
               'Content-Type': 'application/http'
           }
       }
    },

    req2 = {
        url: "/spoons/count",
        options: {
           method: 'GET',
           headers: {
               'Content-Type': 'application/http'
           }
        }
    },
    authToken = "Bearer my_token123",
    batchUrl = "",
    options = {
        method: 'POST',
        headers: {
            'Authorization': authToken,
            'Content-Type': 'multipart/mixed'
        },
        body: {req1, req2}
    };

    return fetch(batchUrl, options)
        .then(response => response.json())
        .then(items => dispatch(batchSuccess(items)))
        .catch((err) => {
            console.log(err)
        });

However it returns an error - bad request. I suppose I may bine HTTP requests in wrong way.

Is there simpler way of doing this?

Where in Network Chrome Dev Tools can I see nested HTTP requests?

My RESTful service allows batching requests.

I'm trying to bine requests into one batch with help of Fetch API:

let req1 = {
        url: "/cups/count",
        options: {
           method: 'GET',
           headers: {
               'Content-Type': 'application/http'
           }
       }
    },

    req2 = {
        url: "/spoons/count",
        options: {
           method: 'GET',
           headers: {
               'Content-Type': 'application/http'
           }
        }
    },
    authToken = "Bearer my_token123",
    batchUrl = "http://something./batch",
    options = {
        method: 'POST',
        headers: {
            'Authorization': authToken,
            'Content-Type': 'multipart/mixed'
        },
        body: {req1, req2}
    };

    return fetch(batchUrl, options)
        .then(response => response.json())
        .then(items => dispatch(batchSuccess(items)))
        .catch((err) => {
            console.log(err)
        });

However it returns an error - bad request. I suppose I may bine HTTP requests in wrong way.

Is there simpler way of doing this?

Where in Network Chrome Dev Tools can I see nested HTTP requests?

Share Improve this question edited Apr 4, 2017 at 0:01 Bergi 667k161 gold badges1k silver badges1.5k bronze badges asked Apr 3, 2017 at 23:30 nicholasnicholas 1001 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Your code does not work because it does not follow multipart/mixed request format:

  1. In Content-Type header, there is no boundary information.
  2. The child requests are not divided by boundary, instead they will be sent as plain text of req1 & req2 object.

In order to send valid multipart/mixed request, there is a node.js module batchelor. According to the introduction page, its usage is pretty simple.

If you want to send multipart/mixed request from browser, you can use build tool (gulp, webpack etc.) to pile batchelor into something like "batchelor-piled.js" and import it in HTML.

For developer tool, I didn't find anything in Chrome, but the child requests are visible in Firefox debug window's "Params" tab.

Here is an example of a batch request using the Fetch API with the Gmail Batch REST API.

This will get the content of several messages at once.

const response = await fetch("https://www.googleapis./batch/gmail/v1", {
  headers: {
    "Content-Type": "multipart/mixed; boundary=batch_boundary",
    Authorization: "Bearer <access_token>",
  },
  method: "POST",
  body: `--batch_boundary
Content-Type: application/http
Content-ID: 1

GET /gmail/v1/users/me/messages/{message-id-1}

--batch_boundary
Content-Type: application/http
Content-ID: 2

GET /gmail/v1/users/me/messages/{message-id-2}

--batch_boundary--`,
});

console.log(await response.text());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745294915a4621081.html

相关推荐

  • javascript - JS Fetching batch data with HTTP - Stack Overflow

    My RESTful service allows batching requests. I'm trying to bine requests into one batch with help

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信