Friday, May 27, 2016

WSO2 ESB Accessing application/x-www-form-urlencoded Data


Problem:

I want to pass application/x-www-form-urlencoded data to the ESB:
curl -v -X POST 'http://myhost/myapi/stuff' -d 'myvar=hello' -H "Content-Type: application/x-www-form-urlencoded"
...and then be able to receive each form variable in my ESB API .
To get a given variable, I'm doing this:
<resource methods="POST" uri-template="/stuff">
      <inSequence>
         <property name="myvar" expression="//xformValues//myvar/text()"></property>
...
Then I'm storing that in an argument:
<args>
   <arg evaluator="xml" expression="$ctx:myvar"></arg>
</args>
Then I'm later trying to stuff it in a payload with $1.
I know that the issue is with the property line:
<property name="myvar" expression="//xformValues//myvar/text()"></property>
...because if I store a literal value in the line, it will work.
I do have the messageReceiver configured in the axis.xml:
<messageFormatter contentType="application/x-www-form-urlencoded"
    class="org.apache.axis2.transport.http.XFormURLEncodedFormatter"/>
<messageFormatter contentType="multipart/form-data"
And, I have the messageBuilder configured in the axis.xml file:
<messageBuilder contentType="application/x-www-form-urlencoded"
    class="org.apache.synapse.commons.builders.XFormURLEncodedBuilder"/>
ANSWER : Just use
`<property name="myvar" expression="//myvar/text()"/>`
e.g
wire logs
[2016-05-27 12:59:22,160] DEBUG - wire >> "POST /foo123 HTTP/1.1[\r][\n]"
[2016-05-27 12:59:22,161] DEBUG - wire >> "User-Agent: curl/7.37.1[\r][\n]"
[2016-05-27 12:59:22,161] DEBUG - wire >> "Host: 192.168.56.1:8280[\r][\n]"
[2016-05-27 12:59:22,161] DEBUG - wire >> "Accept: */*[\r][\n]"
[2016-05-27 12:59:22,161] DEBUG - wire >> "Authorization: Basic QU93ZmJQRW5YcXVDVnhIS1ZZT2ZlVFQxT01NYTpEeGpBY3dodFB5Wm5JTU9CZmJEakRHc21FQjBh[\r][\n]"
[2016-05-27 12:59:22,161] DEBUG - wire >> "Content-Type: application/x-www-form-urlencoded;charset=UTF-8[\r][\n]"
[2016-05-27 12:59:22,161] DEBUG - wire >> "Content-Length: 135[\r][\n]"
[2016-05-27 12:59:22,161] DEBUG - wire >> "[\r][\n]"
[2016-05-27 12:59:22,161] DEBUG - wire >> "grant_type=authorization_code&code=291a24141f1e3332baf9799c4c378033&redirect_uri=https://identity.qa.example.com/playground2/oauth2.jsp"
[2016-05-27 12:59:22,163] DEBUG - LogMediator Start : Log mediator
[2016-05-27 12:59:22,163]  INFO - LogMediator To: /foo123, MessageID: urn:uuid:c20f24d4-668d-447e-9de0-dbe5885b6d16, Direction: request
[2016-05-27 12:59:22,164] DEBUG - LogMediator End : Log mediator
[2016-05-27 12:59:22,164] DEBUG - LogMediator Start : Log mediator
[2016-05-27 12:59:22,164]  INFO - LogMediator payload = <mediate><code>291a24141f1e3332baf9799c4c378033</code><redirect_uri>https://identity.qa.example.com/playground2/oauth2.jsp</redirect_uri><grant_type>authorization_code</grant_type></mediate>,code = 291a24141f1e3332baf9799c4c378033
[2016-05-27 12:59:22,164] DEBUG - LogMediator End : Log mediator
[2016-05-27 12:59:22,165] DEBUG - wire << "HTTP/1.1 200 OK[\r][\n]"
[2016-05-27 12:59:22,165] DEBUG - wire << "Authorization: Basic QU93ZmJQRW5YcXVDVnhIS1ZZT2ZlVFQxT01NYTpEeGpBY3dodFB5Wm5JTU9CZmJEakRHc21FQjBh[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "Host: 192.168.56.1:8280[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "Content-Type: application/x-www-form-urlencoded;charset=UTF-8[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "Accept: */*[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "Date: Fri, 27 May 2016 19:59:22 GMT[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "Server: WSO2-PassThrough-HTTP[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "Transfer-Encoding: chunked[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "91[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "code=291a24141f1e3332baf9799c4c378033&redirect_uri=https%3A%2F%2Fidentity.qa.example.com%2Fplayground2%2Foauth2.jsp&grant_type=authorization_code[\r][\n]"
[2016-05-27 12:59:22,166] DEBUG - wire << "0[\r][\n]"
[2016-05-27 12:59:22,167] DEBUG - wire << "[\r][\n]"
API
<api xmlns="http://ws.apache.org/ns/synapse" name="foo123" context="/foo123">
   <resource methods="POST">
      <inSequence>
         <log></log>
         <property name="payload" expression="//mediate"></property>
         <property name="code" expression="//mediate/code"></property>
         <log level="custom" separator=",">
            <property name="payload" expression="$ctx:payload"></property>
            <property name="code" expression="$ctx:code"></property>
         </log>
         <respond></respond>
      </inSequence>
   </resource>
</api>
 

2 comments:

  1. woah that is too much computer science for my simple brain. even though as layman i could gather enough from this blog that it was pretty descriptive and and i am pretty sure that people who top custom essay services and always in search of extra information willfind this artice truly helpful.

    ReplyDelete
  2. You should console that you design out your alternatives and make an awesome application that suits your prerequisites and requirements. website to app

    ReplyDelete