VMware Cloud Community
Jdeb68
Contributor
Contributor

Passing token as query parameter to REST Host

I'm attempting to pass a token as a query parameter in my POST call. I am not able to get vRO to interpret my query parameter I have configured in my REST Operation call.

Note: I cannot pass the authentication token as a header, as the only allowed headers by this REST API are: Origin, X-Requested-With, Content-Type, Accept

I created the REST Operation as follows that is giving me the error: POST "https://pronghorn.internal/getNetworkStats?token={{token}}"

FYI: Pronghorn (by itential) is the REST Host I am working with.

I am hoping this is simply a syntax issue. I have yet to figure out the proper syntax to make this work however.

My REST Operation is: https:/pronghorn.internal/getNetworkStats?token="{{token}}"

My error log shows the Request URL as: https:/pronghorn.internal/getNetworkStats?token="}"  with a message of "Illegal character in query at index xx.

I have attempted single brackets which errors out with a message token must be passed as a query parameter.

Here is the flow of my workflows as an FYI:

My workflow is as follows: (Using JSON with headers set appropriately)

Get Token Workflow:

  • Capture username and password from input and format into json string.
  • Pass credentials to REST Operation Workflow of "POST 'https://pronghorn.internal/login"
  • Parse response as output attribute called 'token'

Call to Get Network Stats Workflow:

  • Capture network input from user and format into json string (body).
  • Pass json string (body) and token to execute REST Operation Workflow of POST "https://pronghorn.internal/getNetworkStats?token={{token}}"
  • Parse response and display network stats

This attempt fails on attempting the POST.

I have logging enable to verify the steps along the way.

The Request URL shows as: https:/pronghorn.internal/getNetworkStats?token="}"  with a message of "Illegal character in query at index xx.

Any help would be greatly appreciated. I've been banging my head against the wall on this for a while!

Thanks,

Jerry

Reply
0 Kudos
9 Replies
iiliev
VMware Employee
VMware Employee

Hi Jerry,

Have you tried getNetworkStats?token={token} ?

Reply
0 Kudos
Jdeb68
Contributor
Contributor

Yes I have used "?token={token}".

When I set it as this it includes '?token=' but drops \{token}'.

It has me thinking it may be needing escape characters. So I have also tried ?token=%7D{token%7F} but this did not work either. I have tested with escaping with double brackets as well.

I'm thinking I may need to modify the REST Operation Workflow in the Request URL and append the {{token}} parameter. I'm not sure exactly where to do this however.

Thanks.

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

Tested it in my environment.  ?token={token} seems to work.

Which version/build number of vRO and REST plug-in do you use? Also, what do you mean by "includes '?token=' but drops \{token}'"?

Reply
0 Kudos
Jdeb68
Contributor
Contributor

I'm running vRO  7.2.0.4629841

Rest Plugin : REST 2.2.1.4512209

I meant when I added the query parameter to the REST operation WF, as in "/networkstats?token={token}; it ignored everything after the '=" sign.

Thanks!

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

I tested with exactly the same build numbers of vRO and REST plug-in, and ?token={token} worked (launched 'Invoke a REST operation' workflow, provided a value for token input, and verified that the provided value reached REST endpoint).

Reply
0 Kudos
Jdeb68
Contributor
Contributor

Good to know that approach works. What doesn't work is:

Trying to pass the token to a REST Operation workflow (so I can call this rest operation workflow from other workflows). I s

What I am attempting to do is have a single workflow that:

1). Logs in and obtains token.

2). Calls POST or GET call passing the token as a query parameter. 

These are fixed REST operations I am calling as they will be integrated into larger deployment scripts. I don't want user interaction providing the REST Call to be made as this will not be prompt a user should have to be prompted for.

Should I do this a different way?

Thanks!

Jerry

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

Yes, this is doable.

Your workflow should have two workflow elements which will call 'Invoke a REST operation' workflow (the first will call the operation to login and get the token, and second will call the operation to do POST using token as a query parameter). Between these two workflow elements, ad a scriptable task element if some postprocessing is needed, eg. if the token returned from the first token is not its whole response body.

Then, create 2 workflow attributes of type REST:RESTOperation, let's say loginOp and useOp, and set their values to your REST operations. And one more attribute attrToken to store the token value.

Then, you need to setup the bindings for the workflow elements. They could be something like the following:

1) The first workflow element - input binding from loginOp attribute to restOperation input; output binding from contentAsString to attrToken attribute.

2) The scriptable task element - should have attrToken attribute as both input and output. You can write some Javascript code to parse the content of attrToken value, extract the actual token, and assign it again to attrToken

3) The second workflow element - input bindings from useOp attribute to restOperation input and from attrToken attribute to param0 input. You may also need to add more attributes to provide values for content and defaultContentType parameters.

And that's all. When you launch this workflow, it will call the first REST operation, will get its output body, will postprocess it to extract the token, and will call the second REST operation passing the token via param0 input parameter (assuming the token value is the first template parameter in your POST call URL)

Reply
0 Kudos
Jdeb68
Contributor
Contributor

Thanks for your input. I am very appreciative.

I have the workflow working. The issue at heart is this:

The token I receive from the server ends with "%3D%3D"   Here is an example with the middle of the token removed: MT_removed_fw%3D%3D

This means I am passing reserved characters as part of my authentication token in my POST call:  https://prghn.internal.com/getNetStats?token={{token}},

While I am able to do this just fine within a tool such as Postman, when I try to run this within my vRO workflow passing the token attribute, it is errors out.

I have attempted both to pass my token attribute from the workflows into the POST call.

/dns_ipam/getIpamStatistics?token={token}

/dns_ipam/getIpamStatistics?token={{token}}

So In your testing of invoke a rest call and entering a token into the prompt, if that token ended with the same characters as my token does, I imagine it would fail with a similar error.

Thanks,

Jerry

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

The proper syntax to use in RESTOpeation workflows is ?token={token}

There was a recent thread on the same topic; check https://communities.vmware.com/thread/573442

Reply
0 Kudos