question

Martin L avatar image
Martin L asked

calling a 3rd party web hook - translating from node example code to cloudscript

I'm looking for a spot of basic tech help with Cloudscript calling a 3rd party web hook please. I'm following the example given here: https://api.playfab.com/docs/tutorials/landing-automation/making-webhook-calls-from-cloud-script

The Cloudscript http.request is a Playfab implementation, with no documentation other than the page above that I can find, so as a game but non-web developer I'm having trouble translating from demo code that uses built in node.js http.request, as the 2 don't work the same.

It partially works. I can get a response from the web server but I can't work out how to package up the auth part and the param part in the code below. How do I put them in the header and/or body please?

Thanks in advance for the help.

// Cloud Script example from: https://api.playfab.com/docs/tutorials/landing-automation/making-webhook-calls-from-cloud-script
var url = "http://demo.3rdparty.com"; // <- got this piece working fine
var method = "post";
var contentBody = "";
var contentType = "application/json";
var headers = {};
var responseString = http.request(url, method, contentBody, contentType, headers);




// 3rd party demo code, uses node.js http.request
var http = require('http');
// Build the request parameters first
var options = { 
    protocol: "http:",
    hostname: "demo.3rdparty.com",
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    auth: "myKeyHere"  // <- how do I get this piece into the Cloudscipt one?
};
// Make the request
var request = http.request(options, function (response) {
    // Prepare to store the response body
    var chunks = [];
    // Collect the data from the response
    response.on('data', function (data) {
        chunks.push(data);
    });
    // Once we have all the data, assemble it all and output it.
    response.on('end', function () {
        console.log('Response data: ' + chunks.join().toString());
    });
});
// Write the data to the request stream
request.write("{ \"param\": \"Test string\" }"); // <- and this piece?
// Execute the request
request.end();
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

·
brendan avatar image
brendan answered

Auth for an HTTP request is usually a header value. For example, have a look at this thread, where we discuss using http.request to make a call into Firebase: https://community.playfab.com/questions/13133/cloudscript-httprequest-to-firebase-fails.html.

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.