I am considering using HMAC to sign requests and I want to include the URL as well as other things (e.g. HMAC = verb + url + nonce + timestamp + body). I don't see a simple way to get the request URL that the client sent, it looks like I have to reconstitute it using:
var req_verb = context.getVariable('request.verb'); var req_scheme = context.getVariable('client.scheme'); var req_host = context.getVariable('request.header.host'); var req_request_uri = context.getVariable('request.uri'); var req_url = req_scheme + "://" + req_host + req_request_uri;
Since the message URI includes the basepath, suffix and params, can I assume what the client sent is what Apigee actually gets? Or could it change in transit?
Solved! Go to Solution.
var req_verb = context.getVariable('request.verb');
var req_scheme = context.getVariable('client.scheme');
var req_host = context.getVariable('request.header.host');
var req_request_uri = context.getVariable('request.uri');
var req_url = req_scheme + "://" + req_host + req_request_uri;
How about "proxy.url" ?
Scope: Proxy request Type: String Permission: Read
Gets the complete URL associated with the proxy request received by the ProxyEndpoint, including any query parameters present.
@Kurt Kanaskie , Yes, You are right. Missed to verify URL contents. I believe it's a bug. Above URL is no use, I believe this has to be fixed in the product.
Please check http://docs.apigee.com/api-services/reference/variables-reference for complete variable reference .
You can check proxy.url at proxy side or request.url at target scope to get the complete url.
var req_verb = context.getVariable('request.verb');
var req_scheme = context.getVariable('client.scheme');
var req_host = context.getVariable('request.header.host');
var req_request_uri = context.getVariable('request.uri');
var req_url = req_scheme + "://" + req_host + req_request_uri;
That's pretty much what I'm doing, and that message looks dated.
BTW:
I modified my code to use request instead of message, looks more consistent 🙂
Correct. That's what I meant. request.host worked for me and req.get('host'); in Node.js. I'm glad it worked! Don't forget to mark as accepted 😉
Maybe this one could be documented more explicitely under Variables Reference http://docs.apigee.com/api-services/reference/variables-reference.
I'm confused, in my JS code request.host is empty.
print('request.host: ' + context.getVariable('request.host'));
But that's OK, I like the new documentation : )
You got it right from the beginning Kurt. So, you need to use request.header.host.
I added a tip to the variables doc explaining how to construct the request URI, as there is no other way to get this from a built-in flow variable.
Thanks @wwitman!
Nice, glad this worked out.
@Kurt Kanaskie At this point that's the only way to get it. Though I agree that there should be an OOTB variable to get the request url received by Apigee. What you get from the javascript is fine and that is what the client sent. There's no change in transit unless there's some nginx level routing that modified the path or any of the other parameters, which is not very common.
I found another nice way of doing it, get the proxy.url value in the Pre-Proxy flow hook, assign it to a new variable by using an AssignMessage policy and use the new variable wherever needed.
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-SetRequestUrl">
<DisplayName>AM-SetRequestUrl</DisplayName>
<Properties/>
<AssignVariable>
<Name>SF-PreProxy-V1.RequestUrl</Name>
<Ref>proxy.url</Ref>
<Value>ErrorOnCopy</Value>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>
It seems that proxy.url contains the request url before entering the proxy, after that it gets replaced by the target server url so capturing the value before that happens works well.