VMware Cloud Community
TeKilla79
Enthusiast
Enthusiast

vRealize Orchestrator REST API get Cookie

Hello, 

i logged in to an Web Application and I have to use a Cookie for the additional GET Operations.

In Postman i was able to get the Cookie via a Test Script "pm.cookies.get('mycookie')" and the I was able to use the Cookie for the GET Requests.

But in vRO i cant find a way to get the cookie.

Does anyone know how i cant get the cookie??

THANKs, martin

Reply
0 Kudos
8 Replies
eoinbyrne
Expert
Expert

Hi

The returned cookie should be in the headers

 

// assuming the HTTP response in in myRestResponse
var headers = myRestResponse.getAllHeaders(); // returns a Properties object
// print all the headers
for each(var k in headers.keys)
{
    System.log(k + " -+- " headers.get(k);
}
var cookie = headers.get("<Whatever the cookie header name is>");

 

 

- HTH

Reply
0 Kudos
TeKilla79
Enthusiast
Enthusiast

thanks for your answer BUT I looped through the headers and there is no cookie

TeKilla79_0-1674818365378.png

 

Reply
0 Kudos
eoinbyrne
Expert
Expert

If your target Web application requires authentication first then you probably need to make the relevant requests first?

Also, looking at the content of your headers, the App is replying with "text/html" content so it looks like it's assuming your HTTP request is from a browser. This may be what you expect but it's hard to tell. Can you post code and/or details of what you're trying to do?

Reply
0 Kudos
TeKilla79
Enthusiast
Enthusiast

this is the login request

 

 

var restapiurl="/api.php?login=login&loginUsername="+var_username+"&loginPassword="+var_password;
var restOp1 = new RESTOperation("DNS Operation");
restOp1.method = "GET";
restOp1.urlTemplate = restapiurl;
var operation1 = restHost.addOperation(restOp1);
var request = operation1.createRequest(null,null);
System.log("URL:"+request.fullUrl);
request.setHeader("Content-Type","application/json");
request.setHeader("Accept", "application/json");

var response1 = request.execute();
statusCode = response1.statusCode;
System.log("#DNS Login# Status code: " + statusCode);
headers = response1.getAllHeaders();
contentAsString = response1.contentAsString;
if (statusCode >= 400) {
    System.log("Content as string1: " + contentAsString);
} else {
    var keys=headers.keys;
    for (var i in keys ) {
        var name=keys[i];
        System.log("header ["+name+"]: "+headers.get(name));
    }
}

var cookie = headers.get("dnsphpadmin");
System.log("cookie:"+cookie);

 

i uploaded a screenshot from the postman cookie section

Reply
0 Kudos
eoinbyrne
Expert
Expert

I can't see anything amiss with your code there - what content is returned for the request?

I note that you're setting Content-type to "application/json" but the previous screenshot shows the server us returning "text/html" - does this server/application support JSON as a response content type?

 

Reply
0 Kudos
eoinbyrne
Expert
Expert

Although, perhaps your Login request should be "POST" and not "GET" there?

Looking at the examples under here - https://github.com/benapetr/dnsphpadmin/blob/master/examples/ansible/create_record.yml

eoinbyrne_0-1674822529751.png

 

Reply
0 Kudos
TeKilla79
Enthusiast
Enthusiast

hi

the content-type is not that important because i have no body.

then i found an error, i used GET instead of POST but this does not solve the problem

anyway, the "api" uses url parameters for authenticating and that is not so good, so i don't use the api anymore

wish a nice we!

Reply
0 Kudos
eoinbyrne
Expert
Expert

Hmmm, this is a new one (on me at least 😁) Just spotted this on the latest version of RESTHost

eoinbyrne_0-1674830623518.png

So, if I'm understanding this correctly, AFTER you have made the login request and confirmed HTTP Status == 200 then you can get the cookie value from RESTHost which the RESTOperation was created from.

 

 

Reply
0 Kudos