2017-09-23 4 views
0

Ich versuche, mehrteilige/gemischte Anfrage an azure Direct Batch senden (https://msdn.microsoft.com/en-us/library/azure/mt734910.aspx) zu senden. Ich benutze npm Anfrage Modul.Senden Multipart/gemischte Anfrage in Nodejs

Welche Anfrage Ich möchte bilden: -

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

Was ich versucht habe: -

Erster Ansatz: -

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

Fehler: - Erstes Argument muss ein String sein, Puffer, ArrayBuffer, Array oder Array-ähnliches Objekt

Zweite Methode: -

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

Fehler: Direkter Batch Azure Senden Service options.uri ist ein erforderliches Argument

Bitte mir den richtigen und besseren Ansatz vorschlagen multipart/mixed Anfrage zu senden. Dank Sie

+0

In Ihrem ersten Ansatz zu Handles in Puffer konvertieren. const buf1 = neuer Puffer (Handles); –

+0

Hey @ PankajJatav hat die Handles geändert, um const pufferHandle = new Buffer (handles) zu puffern und das bufferHandle im body übergeben, aber es kam mit folgendem Fehler zurück: Konnte mehrteiligen Inhalt der Anfrage nicht lesen –

Antwort

0

In Ihrem ersten Ansatz, wie Sie content-type-application/json im multipart gesetzt, Sie JSON.stringify() Methode verwenden sollten wie unter den Körper in eine JSON-Zeichenfolge zu konvertieren:

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

Weitere Einzelheiten finden Sie https://github.com/request/request#multipartrelated.