Adobe campaign classic REST POST call with JSON as input | Community
Skip to main content
jagadeeshk20781
May 4, 2018
Solved

Adobe campaign classic REST POST call with JSON as input

  • May 4, 2018
  • 5 replies
  • 7845 views

Hello,

i am new to adobe campaign tool, trying to explore the possible ways to

1. send POST REST request to endpoint with JSON as input

2. get the response as JSON.

3. parse the JSON response for usage inside campaign.

i tried with GET REST request, got the response back.

sample code:

var http = new HttpClientRequest("http://jsonplaceholder.typicode.com/posts/1");

http.method = "GET";

http.execute();

var resp = http.response;

logInfo("Response Code"+resp.code);

logInfo("Response body"+resp.body);

Could you please share the sample code for POST call with jSON as input.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Marcel_Szimonisz

Hello jagadeeshk20781937

You can try JS built in JSON

So I would add,

var jsonObj;

try{

jsonObj = JSON.parse(resp.body.toString());

//do whatever you want

//when saving to db

var jsonStr = JSON.stringify(jsonObj);

}catch(e){

     //nok

}

Marcel

5 replies

Marcel_Szimonisz
Community Advisor
Marcel_SzimoniszCommunity AdvisorAccepted solution
Community Advisor
May 8, 2018

Hello jagadeeshk20781937

You can try JS built in JSON

So I would add,

var jsonObj;

try{

jsonObj = JSON.parse(resp.body.toString());

//do whatever you want

//when saving to db

var jsonStr = JSON.stringify(jsonObj);

}catch(e){

     //nok

}

Marcel

jagadeeshk20781
May 8, 2018

yes, but my question on POST method

Example:

var http = new HttpClientRequest("http://reqres.in/api/users");

http.header["Content-Type"] = "application/json";

http.body=JSON.stringify({

"name": "morpheus",

"job": "leader"

});

http.method="POST";

http.execute();

var response=http.response;

logInfo("output code"+response.code);

logInfo("output value"+response.body);

above sample code, i tried to connect the endpoint but it doesnt work. can

you please help me to identify the error if any. thanks.

On Tue, May 8, 2018 at 2:00 PM, marcel.gent.86 <forums_noreply@adobe.com>

Marcel_Szimonisz
Community Advisor
Community Advisor
May 8, 2018

Can you give us error message?

I can imagine that It could be caused by configuration outside adobe like firewall settings etc

Marcel

jagadeeshk20781
May 9, 2018

Hmm. Seems we need to add the endpoint to server and admin is doing it.

when I am getting a JSON object, and also need to get the value

dynamically using key. But the below code works in Plain JS not in

Campaign. It is printing single character for every iteration in the loop.

Could you please help me to the parse JSON object, because the output is

dynamic

Sample Code:

var actualResponse=JSON.stringfy({

"Name": "ERR"

});

var data=JSON.parse(actualResponse);

Object.keys(data).forEach(function(key) {

console.log('Key : ' + key + ', Value : ' + data[key])})

Response

Marcel_Szimonisz
Community Advisor
Community Advisor
May 10, 2018

you have typo in JSON.stringfy({ it is missing i

It works for me :

10/05/2018 13:16:19 js Key : Name, Value : ERR

Marcel