Sendgrid与SAPUI5应用程序的集成在POST上返回“ 400 Bad Request”,但在Postman上有效

时间:2019-01-03 18:31:29

标签: javascript rest cordova sapui5 sendgrid

我正在开发使用UI5库的Cordova混合应用程序。我需要能够从该应用程序作为电子邮件发送,因此我决定使用Sendgrid API,并且由于NodeJS模块存在问题,因此我正在创建一个AJAX请求。我以前已经用Postman测试了该请求,能够成功发送电子邮件。但是,当我尝试从该应用程序执行相同的请求时,我偶然发现状态为“ 400错误的请求”,而没有进一步的信息。下面是代码片段:

            var settings = {
                "async": true,
                "crossDomain": true,
                "url": "https://api.sendgrid.com/v3/mail/send",
                "method": "POST",
                "headers": {
                    "Content-Type": "application/json",
                    "Authorization": "Bearer [REDACTED]",
                    "cache-control": "no-cache"
                },
                "processData": false,
                "data": {
                    "personalizations": [
                        {
                        "to": [
                            {
                                "email": "myemail@gmail.com"
                            }
                        ],
                        "subject": "Contato do app"
                        }
                    ],
                    "from": {
                        "email": "anotheremail@gmail.com"
                    },
                    "content": [
                        {
                            "type": "text/plain",
                            "value":  formatEmail //variable with the email text
                        }
                    ]
                }
            }

            $.ajax(settings).then(
                function success(response, status) {
                    dialog.close();
                    if (status == 202){
                        MessageBox.confirm('Mensagem enviada com sucesso', {
                            actions: [sap.m.MessageBox.Action.OK],
                            onClose: function(sAction){
                                that.onNavBack()
                            }
                        });
                    } else {
                        MessageBox.error('Erro!');
                    }
                },

                function fail(data, status){
                    dialog.close();
                    MessageBox.error('Request failed.  Returned status of ' + status + ' DEBUGDATA: ' + JSON.stringify(data));
                }  
            );

我真的不知道发生这种情况的原因,因为Postman生成的代码段与我的代码非常相似。任何帮助将不胜感激!

预先感谢

1 个答案:

答案 0 :(得分:0)

通常,HTTP 400意味着错误的请求,这意味着您将错误的参数传递给HTTP POST请求。