How to handle the special characters in <FormParam> parameter in the ServiceCallOut policy in OPDK ?

In OPDK, When a ServiceCallout is made using <FormParam>, Apigee should automatically URL encode the parameters without having to do anything. If Apigee doesn't URL encode automatically. Then, we need to check the message-processor.properties file,

conf/http.properties+HTTPClient.urlencode.request.line=false -> This property will not help Apigee to URL encode the request.

(or)

conf/http.properties+HTTPClient.urlencode.request.line=true -> This property will help Apigee to URL encode the request.

So, To handle special characters in <FormParam> parameter in the serviceCallout policy, conf/http.properties+HTTPClient.urlencode.request.line=true, This property will Apigee to URL encode the requests

0 2 866
2 REPLIES 2

Can you give me a specific example that demonstrates the problem you are describing?

A specific example would be, an API proxy that uses ServiceCallout, with the specific ServiceCallout configuration, which illustrates Apigee not url-encoding the form parameters you send to the target of the ServiceCallout. 

Can you give me that?

I tried this just now and in my tests the SC policy does the right encoding on the wire. Maybe I am missing something.

There is a potential source of confusion. There are two layers of encoding we must consider. The URl-encoding of data that is transmitted on the wire, is independent of the XML encoding you may need to perform in the XML configuration for the ServiceCallout! 

As an example, here is a valid ServiceCallout configuration that uses ampersand-encoding of some characters that are special to XML: 

<ServiceCallout name='SC-FormParams'>
  <Request variable='calloutRequest'>
    <Set>
      <FormParams>
        <!-- the characters special to XML must be encoded -->
        <FormParam name='a'>alpha=7&amp;beta=9</FormParam>
        <FormParam name='b'>beta=8/</FormParam>
        <FormParam name='c'>&quot;charlie&quot;</FormParam>
      </FormParams>
      <Verb>POST</Verb>
      <!-- <Path>/additional/path/here/if/desired</Path> -->
    </Set>
  </Request>
  <Response>calloutResponse</Response>
  <HTTPTargetConnection>
    <SSLInfo>
      <Enabled>true</Enabled>
      <IgnoreValidationErrors>true</IgnoreValidationErrors>
    </SSLInfo>
    <Properties>
      <Property name='success.codes'>2xx, 4xx, 5xx</Property>
    </Properties>
    <URL>https://mytarget.net/</URL>
  </HTTPTargetConnection>
</ServiceCallout>

 

Apigee normally URL encoded the parameters without configuring anything. But, If you are configured the “conf/http.properties+HTTPClient.urlencode.request.line=false” property in message-processor.properties, then Apigee will not getting URL encoding some special characters especially in <FormParams>. Then to get URL encode, please configure “conf/http.properties+HTTPClient.urlencode.request.line=true” this property. Then, It will get a URL encoding of special characters especially in <FormParams>.

For Example :

The servicecallout policy configuration:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">

    <DisplayName>Service Callout-1</DisplayName>

    <Properties/>

    <Request clearPayload="true" variable="myRequest">

        <Set>

            <Verb>POST</Verb>

            <Headers>

                <Header name="Content-Type">application/x-www-form-urlencoded</Header>

            </Headers>

            <FormParams>

                <FormParam name="sub">Hello, Fabian having a nice day</FormParam>

                <FormParam name="encode">Hello,%20Fabian%20having%20a%20nice%20day</FormParam>

                <FormParam name="case">wZ9xNh8TE{mNX+[8</FormParam>

            </FormParams>

        </Set>

        <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>

    </Request>

    <Response>calloutResponse</Response>

    <HTTPTargetConnection>

        <Properties/>

        <URL>http://httpbin.org/post</URL>

    </HTTPTargetConnection>

</ServiceCallout>

Here, you can find “Hello,%20Fabian%20having%20a%20nice%20day” and “wZ9xNh8TE{mNX+[8”  values in above servicecallout policy configuration. These <FormParam> parameter values are not URL encoding, when “conf/http.properties+HTTPClient.urlencode.request.line=false” property configured in message-processor.properties file.

Otherwise, Apigee will normally getting URL encoding (or) you need to change the property as “conf/http.properties+HTTPClient.urlencode.request.line=true” from “false” in message-processor.properties, then URL encoding works.

When "conf/http.properties+HTTPClient.urlencode.request.line=false" property is configured in message-processor.properties,

The response as,

{

  "args": {}, 

  "data": "", 

  "files": {}, 

  "form": {

    "case": "wZ9xNh8TE{mNX [8", 

    "encode": "Hello, Fabian having a nice day", 

    "sub": "Hello, Fabian having a nice day"

  }, 

  "headers": {

    "Content-Length": "106", 

    "Content-Type": "application/x-www-form-urlencoded", 

    "Host": "httpbin.org", 

    "X-Amzn-Trace-Id": "Root=1-6181979f-160e072c61f678e97b776a78"

  }, 

  "json": null, 

  "origin": "34.76.144.115", 

  "url": "http://httpbin.org/post"

}