【问题标题】:sending multipart/mixed request in nodejs在nodejs中发送多部分/混合请求
【发布时间】:2017-09-23 15:29:49
【问题描述】:

我正在尝试向 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)
        }

错误:- 第一个参数必须是字符串、缓冲区、ArrayBuffer、数组或类似数组的对象

第二种方法:-

        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 Direct Batch Send Service。 谢谢你

【问题讨论】:

  • 在您的第一种方法中转换为缓冲区中的句柄。 const buf1 = new Buffer(handles);
  • 嘿 @PankajJatav 将句柄更改为缓冲区 const bufferHandle = new Buffer(handles) 并在正文中传递了 bufferHandle 但它返回错误:无法从请求中读取多部分内容

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


【解决方案1】:

在您的第一种方法中,当您在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) 
  }
]

更多详情请见https://github.com/request/request#multipartrelated

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多