2016-03-15 191 views
0

我試圖做一個批處理的HTTP請求(與流星/ HTTP服務器上)以下文的Gmail:Gmail的API返回400錯誤請求與批次請求

batchGetMessages = (accessToken, ids) => { 
    let userId = 'me'; 
    let url = `https://www.googleapis.com/batch`; 
    let boundary = `batch_message_request`; 
    let body = ``; 

    _.each(ids, (id) => { 
    body = `${body} 
    --${boundary} 
    Content-Type: application/http 

    GET /gmail/v1/users/${userId}/messages/${id.id}?format=metadata&fields=id%2Cpayload 
    ` 
    }); 

    body = `${body} 
    --${boundary}--` 

    let options = { 
    headers: { 
     'Authorization': `Bearer ${accessToken}`, 
     'Content-Type': `multipart/mixed; boundary="${boundary}"`, 
    }, 
    content: body, 
    }; 

    let data = HTTP.post(url, options); 
    console.log('data: ', data); 
} 

身體串風看起來像這個:

--batch_message_request 
Content-Type: application/http 

GET /gmail/v1/users/me/messages/15375be281102d3d?format=metadata&fields=id%2Cpayload 

--batch_message_request 
Content-Type: application/http 

GET /gmail/v1/users/me/messages/15366f87db6bdfeb?format=metadata&fields=id%2Cpayload 

--batch_message_request 
Content-Type: application/http 

GET /gmail/v1/users/me/messages/15365d62f152dea2?format=metadata&fields=id%2Cpayload 

--batch_message_request-- 

我的請求總是返回一個400錯誤請求錯誤。我檢查了類似的問題,但一直沒能得到這個工作尚未:

Generating HTTP multipart body for file upload in JavaScript

Gmail REST api batch support for getting messages

Batch request - 400 bad request response

任何幫助或建議,將不勝感激。謝謝!

回答

1

好吧,400 BadRequest錯誤似乎是由字符串模板中的換行符引起的。刪除空格得到的一切工作:

... 
/* 
    the lack of whitespace is impotant in the folllowing string template: 
*/ 
    _.each(ids, (id) => { 
    body = `${body} 
--${boundary} 
Content-Type: application/http 

GET /gmail/v1/users/${userId}/messages/${id.id}? format=metadata&fields=id%2Cpayload` 
    }); 

    body = `${body} 
--${boundary}--`; 
... 

有可能保持你的代碼的縮進,消除空白票友的方式,但我還沒有找呢。