Использование NS http.request для отправки локона

Я пытаюсь использовать нативный скрипт HTTP.request для отправки завитков для push-уведомлений в Firebase. Я проверил завиток, и он работает, однако я получаю ошибку неверного запроса при попытке отправить его через 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);
                });

любая помощь будет принята с благодарностью!

1 ответ

Решение

Я понял, это код, который работал. У меня были некоторые проблемы с форматированием, я надеюсь, что это поможет кому-то в будущем!

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));
                });
Другие вопросы по тегам