VMware Cloud Community
jdbigg
Contributor
Contributor

REST Calls Using JSON

Hey All,

   I am new at using the REST plugin with VRO as well as dealing with JSON calls. I am not a programmer or do I have access to any sort of programming environment outside of basic scripting. So all I am asking is for some pointers to get going in the proper direction. The following is a snippet of json code that I need to use as a REST post but I need to be able to input variables each time I run the workflow. I have already successfully added the REST host that I am posting against:

{

"Message": {

  "ToggleMode": "<Toggle Mode Variable>",   <-- This would need to be a variable to pass either on or off with

  "ToggleSetting": "RTA",  <-- not a variable, will always be static "RTA"

  "Token": "<Token Variable Here>"

}

}

Now, if someone know some helpful websites/tutorials I can read, that actually give examples of POST methods and how to configure variables, I will be glad to read them I try my best to figure this out. It cant be that difficult.

Thanks to anyone willing to lend a hand.

0 Kudos
8 Replies
tschoergez
Leadership
Leadership

Hi,

you can use a scriptable task to create the content of the REST request within javascript.

There are a few blog articles out there that explain the concept:

‌http://www.jonathanmedd.net/2014/09/use-headers-in-a-vco-rest-operation.html

http://purevirtual.eu/2012/11/11/exploring-vco-and-rest-apis/

http://www.vcoportal.de/2015/02/vro-and-the-openstack-admin-api/

Regards,

Joerg

0 Kudos
jdbigg
Contributor
Contributor

Ok, after working with the software support people they gave me some basic javascript to work off of. Here is what I got so far:

var DisableRTA = function(authtoken){

    var request = new Object();
    request.Token = "0799752E825161041D6F3673F7EE768B56A8B1A81EDBEA080C29AB0DB86364B0C8A94C7254517A6A18415A38C35ABB06D580B9E207E71B3B677AD87F5869F1A0";
    request.ToggleMode=Off;
    request.ToggleSetting=RTA;
  

    var requestString = JSON.stringify(wrapRequestObject(request));
    var url = "http://MortgageServicer.fics/MortgageServicerService.svc/REST/ToggleSetting";
        
    $.ajax({
        type: "POST",
        url: url,
        data: requestString,
        async: false,
        success: function(json) {
                DisableRTA(json);
        }
    });
}

I run this and receive no errors back, but it doesn't produce the desired effects on the system that I am running it against, in fact it doesn't seem to do anything at all. Any ideas?

0 Kudos
Dan_Linsley
VMware Employee
VMware Employee

Please take a look at the samples Joerg posted.  The last one in particular should help you out.

I recently posted a similar sample on sample exchange here: https://developercenter.vmware.com/samples?id=935

0 Kudos
jdbigg
Contributor
Contributor

Ok, none of that is helping. I have tried over and over with failure.

Apparently, the following section isn't usable in vRO:

$.ajax({

        type: "POST",

        url: url,

        data: requestString,

        async: false,

        success: function(json) {

                DisableRTA(json);

It seems that vRO doesn't like the "$" as it says its not declared anywhere. I guess my new question is how do I convert that into something vRO will accept?

0 Kudos
iiliev
VMware Employee
VMware Employee

vRO is not a Web UI application and doesn't understand Ajax calls.

Take a look at 'Invoke a REST host' workflow that comes pre-installed out-of-the-box with REST plug-in (under Library -> HTTP-REST Samples folder). And go over the article Joerg suggested - 'Exploring vCO and REST APIs' http://purevirtual.eu/2012/11/11/exploring-vco-and-rest-apis/

Basically, you need to define a RESTHost object, create a request (providing various properties like URL, HTTP method, body if needed, etc), and then execute the request. All steps are shown in the samples/blog articles.

0 Kudos
jdbigg
Contributor
Contributor

Ok All, i am throwing in the towel on this one, nothing i am trying seems to work. Guess i need step by step instructions. Here's what i have done so far.

First of all the method of the REST host i am trying to use is: http://mortgageservicer.fics/MortgageServicerService.svc/REST/ToggleSetting

  1. I ran the "Add a REST Host" work flow with the URL of http://mortgageservicer.fics
  2. I then ran the "Add a REST Opration" selecting the REST host i created in step one, choosing the POST method and a default content type of application/json
  3. From this point forward nothing that i have tried is working, non of the examples you have been so kind to post is making sense to me.
  4. What should have my next steps been? maybe i am not formatting something correctly.
  5. The closest i have gotten it to work was to use the "Invoke a REST operation" work flow. When i use this one, it actually sends the request to the REST host but my input information isnt right and it fails.

In the "Invoke a REST operation" workflow i tried replacing this:

var request = restOperation.createRequest(inParamtersValues, content);


With this

var request = restOperation.createRequest({'ToggleMode':{Off}&'ToggleSetting':{RTA}&'Token':{0799752E825161041D6F3673F7EE768B56A8B1A81EDBEA080C29AB0DB86364B0C8A94C7254517A6A18415A38C35ABB06D580B9E207E71B3B677AD87F5869F1A0}});

And it of course failed

Any ideas?

0 Kudos
jdbigg
Contributor
Contributor

Btw, when i did the "Add a REST operation" i used the URL Template of "/MortgageServicerService.svc/REST/ToggleSetting", and this is a POST type method so i chose "Post".

0 Kudos
jdbigg
Contributor
Contributor

I finally got this to work. I used the "Invoke A REST Operation". In the content section I wasn't formatting my request correctly.

I was doing this:

{"ToggleMode":"Off","ToggleSetting":"RTA","Token":"0799752E825161041D6F3673F7EE768B56A8B1A81EDBEA080C29AB0DB86364B0C8A94C7254517A6A18415A38C35ABB06D580B9E207E71B3B677AD87F5869F1A0"}

But should have been doing this:

{"Message":{"ToggleMode":"Off","ToggleSetting":"RTA","Token":"0799752E825161041D6F3673F7EE768B56A8B1A81EDBEA080C29AB0DB86364B0C8A94C7254517A6A18415A38C35ABB06D580B9E207E71B3B677AD87F5869F1A0"}}

After wrapping it with the "Message" tag it worked just fine. Thank you all who attempted to help.

0 Kudos