What is correct CustomHttpHeaders parameter for GameSparks webhooks for PUN?

hmdeep
hmdeep
edited May 2018 in DotNet
I'm trying to implement Photon GameSparks webhooks.

I can't able to get data with Spark.getData() in GameSparks. How to set 'CustomHttpHeaders' in webhook to make this work?

According to GameSparks,
"Reading POST Data
There are instances where a remote server may not set the content-type header correctly, and the post data are not automatically translated into Spark.getData(). In these cases, the POST requests have an additional "REQUEST_BODY" attribute set into Spark.getData(). This is the body of the HTTP post request as a string, so you can do your own parsing of this value:"

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @hmdeep,

    You don't need CustomHttpHeaders.
    You need to set the HTTP request's payload a.k.a. POST data.
    Do this using one of the overload methods: AuthenticationValues.SetAuthPostData
  • Thank you JohnTube for a reply.

    I have removed CustomHttpHeaders as you suggetsted and and added SetAuthPostData in request.

    Here the code I tried but it didn't work
    var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
    
    		customAuth.AddAuthParameter ("gsid", userID);
    		customAuth.AddAuthParameter ("token", authToken);
    		customAuth.SetAuthPostData("{'Content-Type' : 'application/json'}");
    
    		PhotonNetwork.AuthValues = customAuth;
    Still it's not working. Any suggestion?
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited May 2018
    First I'm sorry I must have confused Webhooks with Custom Authentication.
    Do you need one or the other? Or both?
    Photon Webhooks are HTTP POST requests with "Content-Type" : "application/json" by default.

    In any case, you need to follow these two GameSparks guides: "How to Implement External HTTP Callbacks" and "How to Use Custom Callback Urls"

    For Photon Custom Authentication with GameSparks:

    From your code snippet, you are sending authentication parameters as query string (GET parameters).
    customAuth.AddAuthParameter ("gsid", userID);
    customAuth.AddAuthParameter ("token", authToken);
    This will result in the following request URL:

    ?gsid=<userID>&token=<authToken>


    This line does not make sense though:
    customAuth.SetAuthPostData("{'Content-Type' : 'application/json'}");
    As I told you before you do not need to set custom HTTP headers because you do not need to set the Content-Type to "application/json" because:

    1. You are not sending any JSON payload.
    2. You are not sending any payload. The request will be of type GET.
    3. You are sending authentication credentials as query string parameters ONLY.
    4. GameSparks by default expects "Content-Type" : "multipart/form-data".

    POST parameters are only interpreted into the data object when the content type of the request is "multipart/form-data".


    How to get query string parameters from GameSparks' CloudCode:

    From the request URL:

    ?gsid=<userID>&token=<authToken>
        
        var gsid = Spark.getData().gsid;
        var token = Spark.getData().token;
    
    How to get and parse JSON POST data from GameSparks' CloudCode:
        var rawPostData = Spark.getData().REQUEST_BODY
        var jsonObj = JSON.parse(rawPostData);
    ---

    How to set HTTP headers for webhooks:

    Custom HTTP headers can be set from the dashboard only.
    You should know that some HTTP headers are restricted and can't be set from the dashboard.
    Their values will be skipped if found.
    • Accept
    • Connection
    • Content-Length
    • Content-Type
    • Date
    • Expect
    • Host
    • If-Modified-Since
    • Range
    • Referer
    • Transfer-Encoding
    • User-Agent
    • Proxy-Connection
    ---

    How to force Custom Authentication HTTP requests to use "Content-Type" : "application/json":

    Since some web servers or web apps require "Content-Type" : "application/json" AND NO QUERY STRING, you could enable this behaviour using a special dashboard key/value: "PostJson" = "true".
    So if you add this key/value you will force all authentication HTTP requests for the app to use POST and Content-Type to be "application/json" and any query string or optional/custom dashboard key/values will be converted to JSON object payload or merged with the JSON object sent from client. Also the client should send Dictionary<string, object> payload data.
  • Thank you for a detailed explanation. This is very vital information for noobies like me.
     var rawPostData = Spark.getData().REQUEST_BODY
     var jsonObj = JSON.parse(rawPostData);
    This is exactly what I need.