10 Replies Latest reply on Jun 24, 2014 2:50 PM by shawkins

    Pass form data in http message body using ws translator

    yongxin

      We are using the webservice translator to invoke a REST service, our vdb looks like below:

       

      <model name="command" type="VIRTUAL">

        <metadata type="DDL"><![CDATA[

             CREATE VIRTUAL PROCEDURE request

             (requestURL varchar)

             RETURNS (message varchar(100), jobids varchar(100), success varchar(10))

             AS (

                  select request.* from 

                  (call restBDM.invokeHttp(action => 'POST', endpoint =>  (select restURL || requestURL FROM config.base_url where TenantId = 1))) AS ws,

                  XMLTABLE('Request' PASSING jsonToXml('Request', ws.result) columns

                  Message   string PATH './message',

                  JobIDs  string PATH './jobids',

                  Success   string PATH './success'

             ) AS request

             );

              ]]>

        </metadata>

      </model>

       

      So right now we are not passing any data as part of the request, we want to pass 2 key value pairs as part of the http message body. Assuming that would go into "request" parameter of the invokeHttp procedure, I need some help with how to put that into this VDB definition. The two parameters we need to pass as part of the http message body are:

      clusterid:<value>

      params:<value>

       

      Thanks,

      Yongxin Liu

        • 1. Re: Pass form data in http message body using ws translator
          rareddy

          Why is "endpoint" is defined as "select" statement? That will not work. Some thing like below (note below not validated)

           

                 CREATE VIRTUAL PROCEDURE request (requestURL varchar, cluserid string, params string)
                 RETURNS (message varchar(100), jobids varchar(100), success varchar(10))
                 AS 
                 BEGIN
                      DECLARE STRING VARIABLES.qp = QUERYSTRING('requestURL', command.request.clusterid AS clusterid, command.request.params AS params);
                      select request.* from  (call restBDM.invokeHttp(action => 'POST', endpoint =>  qp)) AS ws,
                      XMLTABLE('Request' PASSING jsonToXml('Request', ws.result) columns
                      Message   string PATH './message',
                      JobIDs  string PATH './jobids',
                      Success   string PATH './success'
                 ) AS request
                END
          

           

           

          Ramesh..

          • 2. Re: Pass form data in http message body using ws translator
            shawkins

            > That will not work.

             

            Why won't that work? A scalar subquery is a valid expression.

             

            > I need some help with how to put that into this VDB definition.

             

            That is simply up to the format that you need.  Ramesh is showing above how this would look as url query string parameters (although the input parameters should not be qualified with command).  Otherwise if what you are looking for is as the message body, then yes simply pass them as the request value:

             

            request=>('clusterid:' || clusterid || '

            params:' || params)

            • 3. Re: Pass form data in http message body using ws translator
              gsinghal

              Thanks Ramesh and Steven.

               

              We actually do need to pass those as part of the message body. The snippet you wrote doesn't have a separator for the two parameters, we tried putting a comma right before params: like below:

              request=>('clusterid:' || clusterid || ',params:' || params)

               

              But that didnt help much, any suggestions?

              • 4. Re: Pass form data in http message body using ws translator
                shawkins

                > But that didnt help much, any suggestions?

                 

                As I don't know what exact format you need or what remote error you are seeing, I can't say for sure.  Do you need a specific encoding or have anything specific from the remote side to indicate what might be amiss?

                • 5. Re: Pass form data in http message body using ws translator
                  rareddy

                  May be a full representation of your request how you need to send would be helpful rather than we guessing?

                  • 6. Re: Pass form data in http message body using ws translator
                    gsinghal

                    Sorry about missing details. So we need the contentType to be set to 'application/x-www-form-urlencoded'

                    And the actual request body will be something like

                    "clusterid=3&params=%7Bkey1%3A%22value1%22%7D"

                     

                    So we make our virtual procedure definition to be like below:

                    CREATE VIRTUAL PROCEDURE request

                           (requestURL string, requestParams string)

                           RETURNS (message varchar(100), jobids varchar(100), success varchar(10))

                           AS (

                                select request.* from

                                (call restBDM.invokeHttp(action => 'POST', endpoint =>  (select restURL || requestURL FROM config.base_url where TenantId = 1), request => requestParams)) AS ws,

                                XMLTABLE('Request' PASSING jsonToXml('Request', ws.result) columns

                                Message   string PATH './message',

                                JobIDs  string PATH './jobids',

                                Success   string PATH './success'

                           ) AS request

                           );

                     

                    So we would pass the url as we did earlier and also pass the fully encoded request message body("clusterid=3&params=%7Bkey1%3A%22value1%22%7D") to this stored procedure. The only missing piece we have now is how do we set the contentType. From looking at the source code and documentation for WS translator the contentType parameter is an output parameter so it doesn't sound like that would help us. Thanks again for your help.

                    • 7. Re: Pass form data in http message body using ws translator
                      rareddy

                      Gaurav,

                       

                      Currently I do not see a way to set ContentType or any custom headers from this interface. I see there is "contentType" as OUT parameter. You could log this as enhancement request. I am not sure if there are any other alternatives?

                       

                      Ramesh..

                      • 8. Re: Pass form data in http message body using ws translator
                        shawkins

                        The trouble with map style arguments is that they generally require a custom construct in SQL, like the xml namespaces used in SQL/XML.  We can either simply add an optional content type parameter or we could have a vararg argument that expects the header values to be specified in pairs.

                        • 9. Re: Re: Pass form data in http message body using ws translator
                          rareddy

                          For simplicity sake, how about adding a extra parameter like

                           

                          call restBDM.invokeHttp(action => 'POST', endpoint =>  qp, headers=>'ContentType=application/json;Accepts=application/text')
                          


                          That way one can add more than single header, just need to be able escape the contents for the delimiter

                           

                          Ramesh..

                          • 10. Re: Pass form data in http message body using ws translator
                            shawkins