I have a policy:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="LoginPAT">
<DisplayName>LoginPAT</DisplayName>
<Request clearPayload="true" variable="loginPATRequest">
<Set>
<Headers>
<Header name="apikey">{private.patClientSecret}</Header>
</Headers>
<Verb>POST</Verb>
<FormParams>
<FormParam name="username">{patId}</FormParam>
<FormParam name="password">{patPassword}</FormParam>
<FormParam name="client_id">{private.patClientId}</FormParam>
<FormParam name="client_secret">{private.patClientSecret}</FormParam>
<FormParam name="grant_type">password</FormParam>
</FormParams>
</Set>
</Request>
<Response>loginPATResponse</Response>
<HTTPTargetConnection>
<LoadBalancer>
<Server name="SAM-API"/>
</LoadBalancer>
<Path>/authentication/v1/Authenticate/PersonalAccessToken</Path>
</HTTPTargetConnection>
</ServiceCallout>
This policy fails when some of the formparam values have special characters. For example, if {patPassword} above contains:
uZ9xNh8FE{mNX+[8
then it fails. If I make the same REST call in Postman, Postman will use the --data-urlencode option in CURL to handle these special characters. How can I handle these special characters in the ServiceCallout policy?
Did you try adding application/x-www-form-urlencoded to the header?
==
<Set
<FormParams>
<FormParam name="username">{patId}</FormParam>
<FormParam name="password">{patPassword}</FormParam>
<FormParam name="client_id">{private.patClientId}</FormParam>
<FormParam name="client_secret">{private.patClientSecret}</FormParam>
<FormParam name="grant_type">password</FormParam>
</FormParams>
<Headers>
<Header name="Content-Type">application/x-www-form-urlencoded</Header>
<Header name="apikey">{private.patClientSecret}</Header>
</Headers>
<Verb>POST</Verb>
</Set>
==
That header is automatically added by Apigee because I am using <FormParm/>. I found the problem:
I am using the OnPrem version of Apigee and had modified the message processor configuration over 2 years ago. I had:
conf/http.properties+HTTPClient.urlencode.request.line=false
in the message-processor.properties file. When I set it back to true, everything is urlencoded correctly.
Now I just have to remember why I set it to false 2 years ago. It isn't in my GIT history.