Search code examples
httpfirebasepush-notificationnativescriptangular2-nativescript

Using NS http.request to send a curl


I am attempting to use the nativescript HTTP.request to send a curl for firebase push notifications. I have tested the curl and it works however I am getting a bad request error when i try to send it via http.request.

Here is the curl code (my key has been substituted for a variable for privacy reasons)

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\"}"

here is my 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);
                });

any help would be greatly appreciated!


Solution

  • I figured it out, this is the code that worked. I had some issues with formatting, I hope this helps someone in the future!

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