cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

REST call: how to use x-www-form-urlencoded authentication

mak1
1-Visitor

REST call: how to use x-www-form-urlencoded authentication

The following command is working:

curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: text/plain' -d 'apiKey=1234' 'https://www.telemetry.com/webservice/token'

But the following code returns empty string.

var request = "https://www.telemetry.com/webservice/token";

var content = new Object();

content.apiKey = '1234';

var headers = new Object();

headers.Accept = "text/plain";

var params = {

headers: headers /* JSON */,

ignoreSSLErrors: true /* BOOLEAN */,

url: request /* STRING */,

content: content /* JSON */,

timeout: 60 /* NUMBER */

};

// result: JSON

var result = Resources["ContentLoaderFunctions"].PostJSON(params);

----------------------------------------------------------------------------------------------------------

How to handle the REST call? how to use x-www-form-urlencoded authentication? How to pass the apiKey?

1 REPLY 1
jamesm1
12-Amethyst
(To:mak1)

Hey Mahboob,

You can post a x-www-form-urlencoded request using the PostText service on the ContentLoaderFunction resource. Here is an example:

var params = {

        headers: {

            "Content-Type": "application/x-www-form-urlencoded"      

         },

        url: "" /* Your auth url*/,

    content: "" /* Your auth content. This is usually a param list like param1=one&param2=two. I usually use Postman to create and test the request, then use it to generate a code snippet for http and this will be the last line. */

};

var result = Resources["ContentLoaderFunctions"].PostText(params);

If you're expecting JSON as a result, you can parse it normally using something like:

result = JSON.parse(result);

Hope this helps!

Announcements


Top Tags