Query parameter - making the API a little cleaner

Not applicable

HI - I have what should be a simple question, but have not found the answer

I am proxying an API that looks like this

http://api.foo.com/cgi-bin/title/isbn?12345

I would like the proxy to look like this

http://api.fooproxy.com/isbn/12345

I have the Default Proxy Endpoint Base Path set as

/isbn

and the Default Target Endpoint URL set as

http://api.foo.com/cgi-bin/title/isbn?

I end up with

http://api.fooproxy.com/isbn But to get a result I need to

http://api.fooproxy.com/isbn/?12345 (note that in the original post the ? was missing.

as opposed to what I want which is

http://api.fooproxy.com/isbn/12345

Thanks in advance

Solved Solved
0 1 419
1 ACCEPTED SOLUTION

Not applicable

You are close.

You would need to define a resource with path /{isbnID}. Your target endpoint would be set to http://api.foo.com/cgi-bin/title/isbn

Then, you would add an Extract Variables policy that gets the value of isbnI:

<URIPath>
    <Pattern ignoreCase="true">/{isbnID}</Pattern>
</URIPath>

Then, you would need to set the path suffix for your target endpoint using an Assign Message policy. You can only set the target URL in the target flow, but we usually decide what the target URL suffix should be in the proxy flow.

<AssignMessage name="setIsbnID">
	<AssignVariable>
		<Name>isbnIDSuffix</Name>
		<Value>?{isbnID}</Value>
	</AssignVariable>
</AssignMessage>

Lastly, in your target request preflow, you can use a JavaScript policy to build the full target URL:

try { 

	target = context.getVariable("target.url") + context.getVariable("isbnIDSuffix") context.setVariable("target.url",target) 

}catch (err) { throw 'Error in Setting Target Url' }

It is important that this last part is done in the target request pre-flow and not the proxy request pre-flow. Otherwise, the change will not be applied and it will not work.

View solution in original post

1 REPLY 1

Not applicable

You are close.

You would need to define a resource with path /{isbnID}. Your target endpoint would be set to http://api.foo.com/cgi-bin/title/isbn

Then, you would add an Extract Variables policy that gets the value of isbnI:

<URIPath>
    <Pattern ignoreCase="true">/{isbnID}</Pattern>
</URIPath>

Then, you would need to set the path suffix for your target endpoint using an Assign Message policy. You can only set the target URL in the target flow, but we usually decide what the target URL suffix should be in the proxy flow.

<AssignMessage name="setIsbnID">
	<AssignVariable>
		<Name>isbnIDSuffix</Name>
		<Value>?{isbnID}</Value>
	</AssignVariable>
</AssignMessage>

Lastly, in your target request preflow, you can use a JavaScript policy to build the full target URL:

try { 

	target = context.getVariable("target.url") + context.getVariable("isbnIDSuffix") context.setVariable("target.url",target) 

}catch (err) { throw 'Error in Setting Target Url' }

It is important that this last part is done in the target request pre-flow and not the proxy request pre-flow. Otherwise, the change will not be applied and it will not work.