question

nathanschmitt7 avatar image
nathanschmitt7 asked

http.request() is throwing an exception

I am trying to use the Azure PlayFab cloud script tool to make a web request to the PlayFab client API in order to add credentials to an account. From my research, it looks like the way to send a web request inside of the PlayFab cloud script is using http.request(), however whenever I use it, the catch portion of my try-catch block runs meaning that the method threw an exception.

Here is that code:

javascript

// Call AddUsernamePassword from CloudScript using the client API
        
// Define the request object for AddUsernamePassword
// Make a POST request to the client API endpoint
var url = "https://4f38a.playfabapi.com/Client/AddUsernamePassword"; // Define the API endpoint URL
        
// Define the request headers
var headers = {
    "Content-Type": "application/json",
    "X-Authorization": sessionTicket //This is what we pulled from args in previously
};
        
//Define data
var data = {
    "Username": username,
    "Email": contactEmail,
    "Password": password
};

//Log stuff
const json_headers = JSON.stringify(headers);
const json_data = JSON.stringify(data);

log.debug(`Headers: ${json_headers}`);
log.debug(`Data: ${json_data}`);

//Attempt the web request
try
{
    // Make the request
    http.request(url, "POST", json_data, json_headers, null, function(error, response) {
                
        log.debug("Web request made!");
                
    });
}
catch (e)
{
    return { success: false, message: "Unable to make web request", data: e.message };
}

When setting the parameters for this request, I verified they contained the proper information by using log.debug() statements. Below are my request headers and data which were logged by the cloud script function and match the data scheme match the documentation:

Headers: {"Content-Type":"application/json","X-Authorization":"SESSION TICKET REDACTED"}
Data: {"Username":"testing12","Email":"contact@bluescreenstudios.net","Password":"abcdefg"}

Because the web request kept failing, I originally assumed that I was entering the parameters wrong, but when I copied the messages from the log.debug() statements back on my client and put them into insomnia, the web request returned 200 OK and was successful.

Insomnia Result

Can somebody please let me know what I am doing wrong?

Helpful Links:

PlayFab Docs | AddUsernamePassword API Route

CloudScript
10 |1200

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

1 Answer

·
Xiao Zha avatar image
Xiao Zha answered

The “headers” doesn’t need to be convert to a JSON string with JSON.stringify() method. And here is an example you can refer to:

 handlers.HttpTest=function(args,context)
 {
 var url = "https://xxxx.playfabapi.com/Client/AddUsernamePassword"; // Define the API endpoint URL
            
 // Define the request headers
 var headers = {
     "X-Authorization": args.sessionTicket //This is what we pulled from args in previously
 };
            
 //Define data
 var data = {
     "Username": args.username,
     "Email": args.contactEmail,
     "Password": args.password
 };
    
 const json_data = JSON.stringify(data);
 var contentType="application/json"
    
 //Log stuff
 log.debug(`Data: ${json_data}`);
    
  // Make the request
 var reponse=http.request(url, "POST", json_data,contentType,headers,);
    
 return {reponse}
 }
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.