2017-08-10 50 views
0

我得到来自SendGrid的Web API,当我试图发送者的要求,SendGrid通过取与一的NodeJS 400错误的请求:SendGrid使用具有的NodeJS取 - 400错误的请求

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) 

的请求在邮差使用相同的有效载荷。

+0

别的,他们在他们的反应,这可能有助于送? 如果请求未正确构建,通常会发送错误请求。您可能需要仔细检查是否从Postman传递完全相同的请求。 – Chnoch

+0

这是我从res.json得到 “错误”:[{ “消息”: “错误的请求”, “现场”:空, “帮助”:空 } ] – davegeo

回答

0

来自node-fetch文档的示例似乎表明您需要在body上使用JSON.stringify()

报价:

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

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

+0

是的,这是问题。我认为这会自动被照顾?使用mailGun时;没有必要进行stringify。 – davegeo