question

fabian-kuhn@gmx.de avatar image
fabian-kuhn@gmx.de asked

Problem with http.request and Headers

Im tryin to use the http.request with a header parameter like below :



var headers = {};
headers["Authorization"] = "Basic KEY";
headers["Content-Type"] = "application/json";
var content = {};

content["en"] ='20 Free Coins when u click here!';
content["de"] ='20 DEFree Coins when u click here!';

var jsonBody = {

app_id: "APP ID",

included_segments: ["All"],

contents: content,

};

var link = "https://onesignal.com/api/v1/notifications";
log.info(JSON.stringify(jsonBody));
log.info(JSON.stringify(headers));

FriendList = http.request(link,"post", JSON.stringify(jsonBody), "text/plain", JSON.stringify(headers));

But i get following Error:

{
"code": 400,
"status": "BadRequest",
"error": "JavascriptException",
"errorCode": 1099,
"errorMessage": "Error: Exception has been thrown by the target of an invocation.\n at http.request (Script Document:2:86) -> nction(url, method, content, contentType, headers) { return playfab.http_reque\n at handlers.sendPush (main.js:97:21)\n at __playfab_internal.invokeHandler (Script Document:4:153)\ninfo : \"{\"app_id\":\"xyz\",\"included_segments\":[\"All\"],\"contents\":{\"en\":\"20 Free Coins when u click here!\",\"de\":\"20 DEFree Coins when u click here!\"}}\"\ninfo : \"{\"Authorization\":\"Basic xyz\",\"Content-Type\":\"application/json\"}\"\n\nScript version: 1.46"
}

If i comment out the header parameter i dont get any Errors, but Onesignal returns that the app id is unkown (since its missing the authorization key).

I deleted the keys in this post ofc but i got the correct keys in my Cloud script.

Any ideas ?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

If you update the headers to be passed in without the stringify, that should fix this for you. So, like this:

FriendList = http.request(link,"post", JSON.stringify(jsonBody), "application/json", headers);

Can you give that a try and let us know if you're still seeing issues?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

fabian-kuhn@gmx.de avatar image
fabian-kuhn@gmx.de answered

I tried it but i get the same Error :(

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Sorry, Fabian - I'm out at Xfest Wed/Thurs of this week, but I can do some additional testing on this Friday, to track down the issue.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

fabian-kuhn@gmx.de avatar image
fabian-kuhn@gmx.de answered

Thank you !

 

If u need additional imformation let me know :)

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Much experimentation later, it's clear that the issue is specifically the Content-Type header in the http.request call. Removing just that makes everything work as expected. I'll do some research here to verify the valid headers for http.request, but can you try making the call with only Content-Type removed from your header array?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

fabian-kuhn@gmx.de avatar image
fabian-kuhn@gmx.de answered

It works !

 

and after changing  "text/plain" to "application/json", i can send push notifications with onesignal ! Thank you :)

 

 

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.