在nodejs中发送multipart / mixed请求

时间:2017-09-23 15:29:50

标签: node.js azure multipart-mixed-replace

我正在尝试将multipart / mixed请求发送到azure Direct Batch Send(https://msdn.microsoft.com/en-us/library/azure/mt734910.aspx)。我正在使用npm请求模块。

我想要形成什么要求: -

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直接批量发送服务发送多部分/混合请求的正确和更好的方法。 谢谢你

1 个答案:

答案 0 :(得分:0)

在第一种方法中,当您在content-type中将application/json设置为multipart时,您应该使用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