2017-09-23 27 views
0

我試圖將多部分/混合請求發送到azure直接批量發送(https://msdn.microsoft.com/en-us/library/azure/mt734910.aspx)。我正在使用npm請求模塊。在nodejs中發送多部分/混合請求

我想要什麼要求,形成: -

POST https://{Namespace}.servicebus.windows.net/{Notification Hub}/messages/$batch?direct&api-version=2015-08 HTTP/1.1 
Content-Type: multipart/mixed; boundary="simple-boundary" 
Authorization: SharedAccessSignature sr=https%3a%2f%2f{Namespace}.servicebus.windows.net%2f{Notification Hub}%2fmessages%2f%24batch%3fdirect%26api-version%3d2015-08&sig={Signature}&skn=DefaultFullSharedAccessSignature 
ServiceBusNotification-Format: gcm 
Host: {Namespace}.servicebus.windows.net 
Content-Length: 431 
Expect: 100-continue 
Connection: Keep-Alive 


--simple-boundary 
Content-Type: application/json 
Content-Disposition: inline; name=notification 

{"data":{"message":"Hello via Direct Batch Send!!!"}} 
--simple-boundary 
Content-Type: application/json 
Content-Disposition: inline; name=devices 

['Device Token1','Device Token2','Device Token3'] 
--simple-boundary-- 

我曾嘗試: -

第一種方法: -

Request({ 
      method: 'POST', 
      uri:'https://{namespace}.servicebus.windows.net/{Notification Hub}/messages/$batch?direct&api-version=2015-08', 
      headers: { 
       Authorization, 
     'Content-Type': 'multipart/mixed; ', 
       'ServiceBusNotification-Format': 'gcm', 
       'x-ms-version': '2015-04' 
      }, 
      multipart: [{ 
       'content-type': 'application/json', 
       body: { 
        data:{"message":"Hello via Direct Batch Send!!!"} 
       } 
      }, { 
       'content-type': 'application/json', 
       body : handles // This is array 
      }] 
     }, (err, res, body) => { 
      console.log('res: ', err, res, body) 
     } 

錯誤: - 第一個參數必須是一個字符串, Buffer,ArrayBuffer,Array或類似陣列的對象

第二種方法: -

 var formData = { 
      data:{"message":"Hello via Direct Batch Send!!!"}, 
      devices: handles // This is array 
     } 
     var options = { 
     uri:'https://{namespace}.servicebus.windows.net/{Notificatin Hub}/messages/$batch?direct&api-version=2015-08', 
     headers: { 
        Authorization, 
      'Content-Type': 'multipart/mixed; ', 
        'ServiceBusNotification-Format': 'gcm', 
        'x-ms-version': '2015-04' 
     } 
     } 
     Request.post({options, formData}, (err, res, body) => { 
      console.log('res: ', err, res, body) 
     }) 

錯誤:options.uri是必需的參數

請建議我正確和更好的方法來發送多部分/混合請求到Azure直接批量發送服務。 感謝您

+0

在你的第一種方法轉換爲緩衝區中的句柄。 const buf1 = new Buffer(handles); –

+0

嘿@PankajJatav改變了句柄來緩衝const bufferHandle = new Buffer(句柄),並在主體中傳遞了bufferHandle,但它返回錯誤:無法從請求中讀取多部分內容 –

回答

0

在你的第一種方法,因爲你在multipart設置content-typeapplication/json,你應該使用JSON.stringify()方法對身體轉換成JSON字符串象下面這樣:

multipart: [ 
    { 
    'content-type': 'application/json', 
    body: JSON.stringify({data: {"message": "Hello via Direct Batch Send!!!"}}) 
    }, 
    { 
    'content-type': 'application/json', 
    body : JSON.stringify(handles) 
    } 
] 

有關詳細信息,請參閱https://github.com/request/request#multipartrelated