0

我正在嘗試使用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

回答

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