简体   繁体   English

在node.js中发送多部分/混合请求

[英]sending multipart/mixed request in nodejs

I am trying to send multipart/mixed request to azure Direct Batch Send ( https://msdn.microsoft.com/en-us/library/azure/mt734910.aspx ). 我正在尝试将multipart / mixed请求发送到azure直接批发送( https://msdn.microsoft.com/zh-cn/library/azure/mt734910.aspx )。 I am using npm request module. 我正在使用npm请求模块。

What request I want to form :- 我要形成什么要求:-

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--

What I have tried :- 我试过的:-

First Approach :- 第一种方法:

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)
        }

Error :- First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object 错误:-第一个参数必须是字符串,Buffer,ArrayBuffer,Array或类似数组的对象

Second Approach :- 第二种方法:

        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)
        })

Error: options.uri is a required argument 错误:options.uri是必需的参数

Please suggest me the correct and better approach to send multipart/mixed request to Azure Direct Batch Send Service. 请向我建议将多部分/混合请求发送到Azure直接批发送服务的正确且更好的方法。 Thanks you 谢谢

In your first approach, as you set content-type to application/json in the multipart , you should use JSON.stringify() method to convert the body to a JSON string like below: 在第一种方法中,在multipart中将content-type设置为application/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) 
  }
]

For more details, see https://github.com/request/request#multipartrelated . 有关更多详细信息,请参见https://github.com/request/request#multipartrelated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM