【问题标题】:Using NS http.request to send a curl使用 NS http.request 发送 curl
【发布时间】:2017-02-16 15:15:58
【问题描述】:

我正在尝试使用 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);
                });

任何帮助将不胜感激!

【问题讨论】:

  • 看来headers 是错误的。根据文档,它应该是 JSON 格式(虽然不确定它应该是数组还是对象)。另外,如果你正在使用 Angular 的 HTTP 功能,为什么不使用它呢?
  • @rrjohnson85 我已将代码调整为这种格式
  • 我很确定您仍然需要JSON.stringify() 内容。看看 Github 中的 HTTP 测试,你就会明白我的意思了。

标签: http firebase push-notification nativescript angular2-nativescript


【解决方案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));
                });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2012-07-08
    • 2022-01-08
    相关资源
    最近更新 更多