使用NS http.request发送卷曲

问题描述:

我正在尝试使用nativescript HTTP.request发送对于Firebase推送通知的卷曲。我已经测试了curl并且它可以工作,但是当我尝试通过http.request发送它时,我收到了错误的请求错误。使用NS http.request发送卷曲

这里是卷曲的代码(我的钥匙已经代替变量出于保护隐私)

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

任何帮助,将不胜感激!

+0

看起来像'headers'是错误的。根据文档,它应该是JSON格式(不知道它应该是一个数组还是对象)。另外,为什么不使用Angular的HTTP功能? – rrjohnson85

+0

@ rrjohnson85我已经调整了代码以使用这种格式 –

+0

我非常肯定你仍然需要'JSON.stringify()'内容。看看Github中的HTTP测试,你会明白我的意思。 – rrjohnson85

我想通了,这是工作的代码。我有格式化方面的一些问题,我希望这可以帮助未来的人!

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