2017-08-10 3 views
0

Ich bin ein 400 Bad Request vom SendGrid Web API bekommen, wenn ich versuche, die folgende Anfrage zu senden via SendGrid mit NodeJS holen:SendGrid mit NodeJS mit Fetch - 400 Bad Request

var emailBody = { 
"personalizations":[ 
    { 
     "to":[ 
      { 
      "email":"[email protected]" 
      } 
     ] 
    } 
], 
"from":{ 
    "email": "[email protected]" 
}, 
"subject": "Send Grid", 
"content": [ 
    { 
     "type":"text/plain", 
     "value": "Send Grid msg" 
    } 
] 
}; 

var emailOptions = { 
method: 'POST', 
headers: { 
    'Authorization': 'Bearer ' + [API_Key], 
    'content-type': 'application/json' 
}, 
body: emailBody 
}; 
fetch(sendGridUrl, emailOptions) 

Der Anfrage funktioniert in Postman mit der gleichen Nutzlast.

+0

Alles andere, dass sie in ihrer Antwort senden, die helfen könnten? Normalerweise wird eine ungültige Anforderung gesendet, wenn die Anforderung nicht ordnungsgemäß erstellt wurde. Vielleicht möchten Sie überprüfen, ob genau dieselbe Anfrage von Postman weitergeleitet wird. – Chnoch

+0

Dies ist, was ich von der res.json "Fehler" erhalten: [ { "message": "Bad Request", "Feld": null, "help": null } ] – davegeo

Antwort

0

Beispiel aus der node-fetch Dokumentation scheint anzugeben, dass Sie JSON.stringify() auf dem body verwenden müssen.

Zitat:

var body = { a: 1 }; 
fetch('http://httpbin.org/post', { 
    method: 'POST', 
    body: JSON.stringify(body), 
    headers: { 'Content-Type': 'application/json' }, 
}) 
    .then(res => res.json()) 
    .then(json => console.log(json)); 

Von: https://github.com/bitinn/node-fetch#usage

+0

Ja, das war das Problem. Ich dachte, das wäre automatisch erledigt worden? Bei Verwendung von mailGun; es gibt keine Notwendigkeit für die stringify. – davegeo