0

I am trying to send multipart/mixed request to azure Direct Batch Send (https://msdn.microsoft.com/en-us/library/azure/mt734910.aspx). I am using npm request module.

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

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

Please suggest me the correct and better approach to send multipart/mixed request to Azure Direct Batch Send Service. Thanks you

Mofi
  • 46,139
  • 17
  • 80
  • 143
Vikas Chandra
  • 565
  • 1
  • 9
  • 22
  • In your first approach convert to handles in buffer. const buf1 = new Buffer(handles); – Pankaj Jatav Sep 23 '17 at 15:50
  • Hey @PankajJatav changed the handles to buffer const bufferHandle = new Buffer(handles) and passed the bufferHandle in the body but it returned with error : Could not read multipart content from the request – Vikas Chandra Sep 23 '17 at 16:18

1 Answers1

1

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',
    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.

Aaron Chen
  • 9,835
  • 1
  • 16
  • 28