使用 NS http.request 发送 curl

Using NS http.request to send a curl

我正在尝试使用 nativescript HTTP.request 为 firebase 推送通知发送 curl。我已经测试了 curl 并且它可以工作但是当我尝试通过 http.request.

发送它时我收到一个错误的请求错误

这是 curl 代码(出于隐私原因,我的密钥已替换为变量)

curl -X POST --header "Authorization: key=MyKey" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"sound\": \"default\"}, \"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"to\":\"d1LHpypPxA0:APA91bHG4HlZQnb7F_BkZAZYHv1MM00FhNbsONMhsLRB-U4p3As3C0Pp_8ALqQFusOOkgdSHZUlOfHbtt6qXU8pzCnjC-ozfMU3vTqjY0iy90XDvGHkDt0qw1w2wnr73PjFqViHEGONH\"}"

这是我的 http.request

http.request({
                    url: 'https://fcm.googleapis.com/fcm/send',
                    method: "POST",
                    headers: { 'Authorization': 'key=MyKey','Content-Type': 'application/json'} ,
                    content: {
                        "notification": {
                            "title": "testingtesting",
                            "text": "some text",
                            "sound": "default",
                            "priority": "High"
                        }
                    },
                        data: { "foo": "bar" },
                        to: "d1LHpypPxA0:APA91bHG4HlZQnb7F_BkZAZYHv1MM00FhNbsONMhsLRB-U4p3As3C0Pp_8ALqQFusOOkgdSHZUlOfHbtt6qXU8pzCnjC-ozfMU3vTqjY0iy90XDvGHkDt0qw1w2wnr73PjFqViHEGONH"


                }).then((response) => {
                    //HttpResult = response.content.toJSON();
                    console.log('----------------------------------------------------');
                    console.log(response.content);
                }, (e) => {
                    console.log("Error occurred " + e);
                });

任何帮助将不胜感激!

我明白了,这是有效的代码。我在格式化方面遇到了一些问题,希望这对以后的人有所帮助!

var HttpResult;
                http.request({
                    url: 'https://fcm.googleapis.com/fcm/send',
                    method: "POST",
                    headers: { 'Authorization': 'key=MyKey', 'Content-Type': 'application/json' },
                    content: JSON.stringify({
                        "notification": {
                            "title": "testingtesting",
                            "text": "some text",
                            "sound": "default",
                            "priority": "High"
                        },
                         'data': { "foo": "bar" },
                        'to': "d1LHpypPxA0:APA91bHG4HlZQnb7F_BkZAZYHv1MM00FhNbsONMhsLRB-U4p3As3C0Pp_8ALqQFusOOkgdSHZUlOfHbtt6qXU8pzCnjC-ozfMU3vTqjY0iy90XDvGHkDt0qw1w2wnr73PjFqViHEGONH"
                    })
                }).then((response) => {
                    //HttpResult = response.content.toJSON();
                    console.log('----------------------------------------------------');
                    console.log(JSON.stringify(response));
                }, (e) => {
                    console.log("Error occurred " + JSON.stringify(e));
                });