question

Jonas Johnsson avatar image
Jonas Johnsson asked

http.request Cloud Script

i get this error when i try to use http.request

{
    "code": 200,
    "status": "OK",
    "data":
    {
        "ActionId": "sendPush",
        "Version": 1,
        "Revision": 405,
        "Results": 0,
        "ResultsEncoded": "0",
        "ActionLog": "info : "err": "The best overloaded method match for 'PlayFab.LogicServer.Source.PlayFabAPI.http_request(string, string, string, string, string)' has some invalid arguments"
",
        "ExecutionTime": 0.0700015
    }
}

and my code is

function mySendPushNotification(args){
//using one signal API to send push

var result = 0;

// //send notif to player that must do turn
// var params1 = {};
//
// params1.Recipient = args.Recipient;
// params1.Subject = args.Subject;
// params1.Message = args.Message;
//
// try {
// server.SendPushNotification( params1 );

// }
// catch(err) {
// result = 0;
// }

//get the token from data
var params = {};

params.PlayFabId = args.Recipient;
params.data = {};
params.data = [ "Token" ];

userData = server.GetUserData( params );

if(!isObjectEmpty(userData.Data))
{       
    if ( userData.Data[ "Token" ] != null ) {
        _token = JSON.parse(userData.Data[ "Token" ].Value);
        if (_token.enabled != false ){
            switch(_token.platform) {
                case 0://IOS device
                    //send notif to player that must do turn
                    var params1 = {};

                    params1.Recipient = args.Recipient;
                    params1.Subject = args.Subject;
                    params1.Message = args.Message;

                    try {
                        server.SendPushNotification( params1 );
                        result = 1;
                    }
                    catch(err) {                            
                    }           
                    break;
                case 1:
                    var headers = {};
                    headers["Content-Type"] = "application/json";
                    headers["Authorization"] = "key="+ GCM_key;

                    var _contentBody = {
                        "to" : _token.token,    
                        "data" : {
                           "alert":{
                             "title": "End Turn",
                             "body": "body lalala.",
                             "number": 1
                           },
                           "BaliFied":{
                             "Scene":"GamePlay",
                             "PlayfabId":"PlayfabId",
                             "roomId":"roomId",
                           }
                        }
                      };
                    try{
                        var response = http.request(
                            "https://gcm-http.googleapis.com/gcm/send",
                            "POST",
                            _contentBody,
                            "application/json",
                            headers
                        );
                        result = 1;
                        log.info("response", response);
                    }catch(err) {
                        log.info("err", err.message);
                    };
                    break;
            }
        }else{
        }
    };
};

// var url = "https://onesignal.com/api/v1/notifications";
// //var headers = {};
// var _contentType = "application/json";
// var _type = "application/json";
//

// var _headers = JSON.stringify({
// "Content-Type": "application/json",
// "Authorization": "Basic " + RESTAPIKey
// });
//
// var _contentBody = JSON.stringify({
// "app
id": AppID,
// "contents": {en: args.Message},
// "headings": "BaliFied",
// "included
segments": ["All"]
// });
//

// var response = null;
// //response = http.request(_url, "POST", _contentBody, _type, _headers);
// try{response = http.request(_url,"POST",_contentBody,_type,_headers);
// }catch(err) {
// log.info("err", err.message);
// }
//
// log.info("response", response);
return result;

}

any updates for this cloud script?

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

Hi Jonas,

The problem here is that neither the _contentBody nor the headers are strings - they're objects. Can you try using Stringify to turn those into strings and re-test?

Brendan

10 |1200

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

Jonas Johnsson avatar image
Jonas Johnsson answered

here is the code that i changed already with stringify

function mySendPushNotification(args){
//using one signal API to send push

var result = 0;

// //send notif to player that must do turn
// var params1 = {};
//
// params1.Recipient = args.Recipient;
// params1.Subject = args.Subject;
// params1.Message = args.Message;
//
// try {
// server.SendPushNotification( params1 );

// }
// catch(err) {
// result = 0;
// }

//get the token from data
var params = {};

params.PlayFabId = args.Recipient;
params.data = {};
params.data = [ "Token" ];

userData = server.GetUserData( params );

if(!isObjectEmpty(userData.Data))
{       
    if ( userData.Data[ "Token" ] != null ) {
        _token = JSON.parse(userData.Data[ "Token" ].Value);
        if (_token.enabled != false ){
            switch(_token.platform) {
                case 0://IOS device
                    //send notif to player that must do turn
                    var params1 = {};

                    params1.Recipient = args.Recipient;
                    params1.Subject = args.Subject;
                    params1.Message = args.Message;

                    try {
                        server.SendPushNotification( params1 );
                        result = 1;
                    }
                    catch(err) {                            
                    }           
                    break;
                case 1:
                    var headers = {};
                    headers["Content-Type"] = "application/json";
                    headers["Authorization"] = "key="+ GCM_key;

                    var _contentBody = {
                        "to" : _token.token,    
                        "data" : {
                           "alert":{
                             "title": "End Turn",
                             "body": "body lalala.",
                             "number": 1
                           },
                           "BaliFied":{
                             "Scene":"GamePlay",
                             "PlayfabId":"PlayfabId",
                             "roomId":"roomId",
                           }
                        }
                      };
                    try{
                        var response = http.request(
                            "https://gcm-http.googleapis.com/gcm/send",
                            "POST",
                            JSON.stringify(_contentBody),
                            "application/json",
                            headers
                        );
                        result = 1;
                        log.info("response", response);
                    }catch(err) {
                        log.info("err", err.message);
                    };
                    break;
            }
        }else{
        }
    };
};

// var url = "https://onesignal.com/api/v1/notifications";
// //var headers = {};
// var _contentType = "application/json";
// var _type = "application/json";
//

// var _headers = JSON.stringify({
// "Content-Type": "application/json",
// "Authorization": "Basic " + RESTAPIKey
// });
//
// var _contentBody = JSON.stringify({
// "app
id": AppID,
// "contents": {en: args.Message},
// "headings": "BaliFied",
// "included
segments": ["All"]
// });
//

// var response = null;
// //response = http.request(_url, "POST", _contentBody, _type, _headers);
// try{response = http.request(_url,"POST",_contentBody,_type,_headers);
// }catch(err) {
// log.info("err", err.message);
// }
//
// log.info("response", response);
return result;

}

and the result that i get from that is

{
"code": 200,
"status": "OK",
"data": {
"ActionId": "sendPush",
"Version": 1,
"Revision": 408,
"Results": 0,
"ResultsEncoded": "0",
"ActionLog": "info : \"err\": \"Exception has been thrown by the target of an invocation.\"\n",
"ExecutionTime": 0.0909958
}
}

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

Hi again,

So that means that the call was made, but that the other service returned an error. I would have to recommend trying the GCM Push call locally on your machine - I recommend using Postman, as it's a quick and easy way to form web API calls - to test the specific fields you're sending in, and make sure that it works as expected.

Brendan

10 |1200

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

Jonas Johnsson avatar image
Jonas Johnsson answered

here is the result whn i use web API call
http://postimg.org/image/qhyjv3961/

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

Hi Jonas,

It looks like the headers are still being passed as an object, rather than a string - can you try updating that, and re-test?

10 |1200

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

Jonas Johnsson avatar image
Jonas Johnsson answered

here is my code now, with converted header

function mySendPushNotification(args){
//using one signal API to send push

var result = 0;

// //send notif to player that must do turn
// var params1 = {};
//
// params1.Recipient = args.Recipient;
// params1.Subject = args.Subject;
// params1.Message = args.Message;
//
// try {
// server.SendPushNotification( params1 );

// }
// catch(err) {
// result = 0;
// }

//get the token from data
var params = {};

params.PlayFabId = args.Recipient;
params.data = {};
params.data = [ "Token" ];

userData = server.GetUserData( params );

if(!isObjectEmpty(userData.Data))
{       
    if ( userData.Data[ "Token" ] != null ) {
        _token = JSON.parse(userData.Data[ "Token" ].Value);
        if (_token.enabled != false ){
            switch(_token.platform) {
                case 0://IOS device
                    //send notif to player that must do turn
                    var params1 = {};

                    params1.Recipient = args.Recipient;
                    params1.Subject = args.Subject;
                    params1.Message = args.Message;

                    try {
                        server.SendPushNotification( params1 );
                        result = 1;
                    }
                    catch(err) {                            
                    }           
                    break;
                case 1:
                    var headers = {};
                    headers["Content-Type"] = "application/json";
                    headers["Authorization"] = "key="+ GCM_key;

                    var _contentBody = {
                        "to" : _token.token,    
                        "data" : {
                           "alert":{
                             "title": "End Turn",
                             "body": "body lalala.",
                             "number": 1
                           },
                           "BaliFied":{
                             "Scene":"GamePlay",
                             "PlayfabId":"PlayfabId",
                             "roomId":"roomId",
                           }
                        }
                      };
                    try{
                        var response = http.request(
                            "https://gcm-http.googleapis.com/gcm/send",
                            "POST",
                            JSON.stringify(_contentBody),
                            "application/json",
                            JSON.stringify(headers)
                        );
                        result = 1;
                        log.info("response", response);
                    }catch(err) {
                        log.info("err", err.message);
                    };
                    break;
            }
        }else{
        }
    };
};

// var url = "https://onesignal.com/api/v1/notifications";
// //var headers = {};
// var _contentType = "application/json";
// var _type = "application/json";
//

// var _headers = JSON.stringify({
// "Content-Type": "application/json",
// "Authorization": "Basic " + RESTAPIKey
// });
//
// var _contentBody = JSON.stringify({
// "app
id": AppID,
// "contents": {en: args.Message},
// "headings": "BaliFied",
// "included
segments": ["All"]
// });
//

// var response = null;
// //response = http.request(_url, "POST", _contentBody, _type, _headers);
// try{response = http.request(_url,"POST",_contentBody,_type,_headers);
// }catch(err) {
// log.info("err", err.message);
// }
//
// log.info("response", response);
return result;

}

and the result is

{
"code": 200,
"status": "OK",
"data": {
"ActionId": "sendPush",
"Version": 1,
"Revision": 409,
"Results": 0,
"ResultsEncoded": "0",
"ActionLog": "info : \"err\": \"Exception has been thrown by the target of an invocation.\"\n",
"ExecutionTime": 0.3170009
}
}

10 |1200

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

Jonas Johnsson avatar image
Jonas Johnsson answered

any updates?

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, Jonas - I've been trying to replicate your results, but I'm having little luck. Could you send a private mail to devrel@playfab.com with your key and token info, so that we can try to replicate this exactly?

10 |1200

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

Jonas Johnsson avatar image
Jonas Johnsson answered

i've sent you the token,
thanks

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

I've responded by email. Let's continue to iterate on this in email, and we can post the full solution info in this thread when we're done.

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.