VMware Cloud Community
APJ_vm
Enthusiast
Enthusiast

REST Request - content-length

Hi

I am trying to execute a rest request ( POST ) to mount and ISO image on a HP ilo. I have some working powershell code and you have to set the content-length. How can I set the content-length in VRO, when I try to use setheader it errors out saying "Cannot execute the request: ; Content-Length header already present"

Thanks 

Reply
0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee

Hi,

Could you show the code you use to make REST request?

In most cases, content length can be computed from the supplied request body, so you don't need to set the HTTP header explicitly (but this depends on many factors).

Reply
0 Kudos
APJ_vm
Enthusiast
Enthusiast

So after reading your reply I tweaked my code and now it works. Below is the working code, still needs to be cleaned up after being hacked together using snippets from the forum. Basically I was setting the content length forcefully and now I am just skipping that altogether.

Scriptable task to get the session token - retHeader contained the session token

var content = {

"UserName": UserName,

"Password": Password

}

var body = JSON.stringify(content);

var request = restHost.createRequest("POST", restHost.url +"/SessionService/Sessions/", body);

request.contentType = "application/json";

var resp = request.execute()

result = new Properties();

result.put("statusCode", resp.statusCode);

result.put("contentLength", resp.contentLength);

result.put("headers", resp.getAllHeaders());

result.put("contentAsString", resp.contentAsString);

retHeader = result.headers;

Scriptable task to mount the ISO

var insertMedia = {

"Action": "InsertVirtualMedia",

"Target":"/Oem/Hp",

"Image":ImagePath,

"Intent":"",

"Signature":""

}

var body = JSON.stringify(insertMedia);

var request = restHost.createRequest("POST", restHost.url + "/Managers/1/VirtualMedia/2/", body);

request.contentType = "application/json";

for each (var key in retHeader.keys){

if(key != "Content-length"){

    System.log("Key Name: " + key + ", Key Value: " + retHeader.get(key));

request.setHeader(key, retHeader.get(key))

}

}

var resp = request.execute()

System.log(resp.statusCode);

Reply
0 Kudos