Solved

REST API POST with custom body

  • 21 July 2022
  • 6 replies
  • 207 views

Hi,

To make sure my API will always function without manually changing the API token each period, I want to request the token and inject it into an API GET header. To do this, I need to know how to write the proper XML that can do the following:

API URL: example.company.com/api/Login
Required body: EMAIL: example@gmail.com
PASSWORD: examplepassword123

How do I write the poper XML inside a DSD file to request the token using the body parameters (not in the header), I cannot find the proper documentation. Thanks in advance!

icon

Best answer by fwagner 26 July 2022, 12:03

View original

6 replies

Userlevel 2
Badge +3

Hi Robin,

thank you for your question!

Basically you need to combine two techniques: "nesting" of calls (first the login, then the actual call), this can be done as is described in this KB article:

https://legacysupport.timextender.com/hc/en-us/articles/360052383191-Creating-and-using-RSD-files-for-CData-providers#new-method

And secondly you need to send data in the body, just like you described. Please see this example, that sending a SOAP request in the body:

<api:script xmlns:api="http://apiscript.com/ns?v1" xmlns:xs="http://www.w3.org/2001/XMLSchema">    <!-- See Column Definitions to specify column behavior and use XPaths to extract column values from XML. -->    <api:info title="2" desc="Generated schema file." xmlns:other="http://apiscript.com/ns?v1">        <!-- You can modify the name, type, and column size here.   -->        <attr name="Field1"  xs:type="string"  readonly="false"  other:xPath="/item/Field1"     />        <attr name="Field2"  xs:type="string"  readonly="false"  other:xPath="/item/Field2"        />        <attr name="Field3"  xs:type="string"  readonly="false"  other:xPath="/item/Field3"     />    </api:info>    <api:set attr="DataModel" value="DOCUMENT" />    <api:set attr="PushAttributes" value="true" />    <api:set attr="EnablePaging" value="true" />    <api:set attr="Header:Name#" value="SOAPAction"/>    <api:set attr="URI" value="https://your.soap.endpoint.com/?wsdl" />    <api:set attr="XPath" value="/item" />    <api:set attr="method" value="POST"/>    <api:set attr="Header:Name" value="Content-Type" />    <api:set attr="Header:Value" value="text/xml" />    <api:set attr="data">        <?xml version="1.0" encoding="utf-8"?>        <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:uen="uen:esws">            <soapenv:Header/>            <soapenv:Body>                <uen:Esws.getUsers soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">                    <p_companyID xsi:type="xsd:string">[_connection.Password]</p_companyID>                    <p_page xsi:type="xsd:string"></p_page>                    <p_locationID xsi:type="xsd:string">3</p_locationID>                    <p_user_id xsi:type="xsd:string"></p_user_id>                </uen:Esws.getUsers>            </soapenv:Body>        </soapenv:Envelope>    </api:set>    <api:set attr="XPath" value="/Envelope/Body/" />    <!-- The GET method corresponds to SELECT. Here you can override the default processing of the SELECT statement. The results of processing are pushed to the schema's output. See SELECT Execution for more information. -->    <api:script method="GET">        <api:set attr="method" value="GET"/>        <api:call op="xmlproviderGet">            <api:push/>        </api:call>    </api:script>

CData is having helpful additional documentation to get a better understanding of how the RSD files work:

It's not the easiest topic, but I hope this helps you to dig deeper.

Please let us know how it goes!

 

Userlevel 2
Badge +3

Hi Robin,

had some more time to experiment with you example - it's not perfect (doesn't send the token from POST call yet in the nested GET call), but a RSD like this contains already a lot for your case:

<api:script xmlns:api="http://apiscript.com/ns?v1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- See Column Definitions to specify column behavior and use XPaths to extract column values from JSON. -->
<api:info title="JSONData" desc="Generated schema file." xmlns:other="http://apiscript.com/ns?v1">
  <!-- You can modify the name, type, and column size here. -->
  <!-- your fields here -->
</api:info>
<api:set attr="DataModel" value="DOCUMENT" />
<api:set attr="EnablePaging" value="true" />
<!-- The GET method corresponds to SELECT. Here you can override the default processing of the SELECT statement. The results of processing are pushed to the schema's output. See SELECT Execution for more information. -->
<api:script method="GET">
  <api:set attr="in1.method" value="POST" />
  <api:set attr="in1.PushAttributes" value="true" />
  <api:set attr="in1.Header:Name" value="Content-Type" />
  <api:set attr="in1.Header:Value" value="text/plain" />
  <api:set attr="in1.URI" value="[_connection.URI]/requestToken" />
  <api:set attr="in1.EnablePaging" value="false" />
  <api:set attr="in1.JSONPath" value="$." />
  <api:set attr="in1.data">EMAIL: example@gmail.com
PASSWORD: examplepassword123</api:set>
  <api:call op="jsonproviderGet" in="in1">
    <api:push />
    <api:set attr="_out1.URI" value="[_connection.URI]" />
    <api:call op="jsonproviderGet" in="_out1">
      <api:push />
    </api:call>
  </api:call>
</api:script>
</api:script>

hope that helps.

Hi Frank, 

Thanks for the responses! I haven't been able to respond due to my holiday, apologies for that. I will be testing out the solutions you provided ASAP.

 

Userlevel 5
Badge +5

Hi Robin

If you can't make it work with the suggested options above, send a ticket to our support system. I will look at it.

Create ticket

I have some experience with custom post calls in RSD files and if necessary I can contact CData with elevated support.

Userlevel 5
Badge +5

Hi Robin

I will set this to complete as you got a working file created.
Any things that were done to make this work that is not explained in other locations will be added to my RSD guide eventually.

Cool! Hopefully it will help others find their solution.

Reply