skip to Main Content

I need help to create api Proxies that join 2 services Zoho Creator and ebay Trading API

Zoho Creator have possibility to send https POST with json but constructor in Zoho doesn’t support multilayer json (only simple pair), eBay trading api accept only https XML

I send simple json request

{“XMLDATA”:”<GeteBayOfficialTimeRequest xmlns="urn:ebay:apis:eBLBaseComponents">...}

i need to convert this into XML request with value of XMLDATA in the body
and change type of Content-type: in the header to “text/xml”

When i post request from Zoho creator, i obtain in apigee
Content-type: application/x-www-form-urlencoded
Content-length: 234
Content:
XMLDATA=%3CGeteBayOfficialTimeRequest+xmlns%3D%22urn%3Aebay%3Aapis%3AeBLBaseComponents%22%3E+%3CRequesterCredentials%3E+%3CeBayAuthToken%3EMyToken%3C%2FeBayAuthToken%3E+%3C%2FRequesterCredentials%3E+%3C%2FGeteBayOfficialTimeRequest%3E

Value of XMLDATA – url encoded.

Can someone help me how to get it work.

On exit i need:

Content-type: text/xml

Content like:

<?xml version="1.0" encoding="utf-8"?><GeteBayOfficialTimeRequest xmlns="urn:ebay:apis:eBLBaseComponents"><RequesterCredentials><eBayAuthToken>

4

Answers


  1. Chosen as BEST ANSWER

    well i get it works. In Zoho Creator

    headerMap = map();
    headerMap.put("X-EBAY-API-COMPATIBILITY-LEVEL", "855");
    headerMap.put("X-EBAY-API-DEV-NAME", "...");
    headerMap.put("X-EBAY-API-APP-NAME", "...");
    headerMap.put("X-EBAY-API-CERT-NAME", "...");
    headerMap.put("X-EBAY-API-SITEID", "3");
    headerMap.put("X-EBAY-API-CALL-NAME", "GetItem");
    token = “...”;
    reqMap = map();
    reqMap.put("token", token);
    reqMap.put("xmlns", "urn:ebay:apis:eBLBaseComponents");
    reqMap.put("body", "<ItemID>...itemid...</ItemID>");
    url = ".....apigeeurl......";
    resp = postUrl(url, reqMap, headerMap);
    

    1) ExtractVariables Policy

    <ExtractVariables async="false" continueOnError="false" enabled="true" name="extractform">
    <DisplayName>ExtractVar</DisplayName>
    <FaultRules/>
    <Properties/>
    <Header name="X-EBAY-API-CALL-NAME">
        <Pattern ignoreCase="false">{CallName}</Pattern>
    </Header>
    <FormParam name="token">
        <Pattern>{token}</Pattern>
    </FormParam>
    <FormParam name="xmlns">
        <Pattern>{xmlns}</Pattern>
    </FormParam>
      <FormParam name="body">
        <Pattern>{body}</Pattern>
    </FormParam>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <Source clearPayload="false">request</Source>
    </ExtractVariables>
    

    2) AssignMessage Policy to create new POST

    <AssignMessage async="false" continueOnError="false" enabled="true" name="getebayofficialtime">
    <DisplayName>FormXml</DisplayName>
    <FaultRules/>
    <Properties/>
    <Set>
        <Headers>
            <Header name="Content-type">text/xml</Header>
        </Headers>
        <Payload>
    &lt;?xml version="1.0" encoding="utf-8"?&gt;
    &lt;{CallName}Request xmlns="{xmlns}"&gt;
    &lt;RequesterCredentials&gt;
    &lt;eBayAuthToken&gt;{token}&lt;/eBayAuthToken&gt;
    &lt;/RequesterCredentials&gt;
    {body}
    &lt;/{CallName}Request&gt;
    </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
    </AssignMessage>
    

  2. Looks pretty straight forward but let me know if I’m missing anything.

    1) ExtractVariables Policy

    If your inbound payload looks like this:

    {“XMLDATA”:”<GeteBayOfficialTimeRequest xmlns="urn:ebay:apis:eBLBaseComponents">...}
    

    Then do a JSON extraction like this:

    <JSONPayload>
        <Variable name="xmldata">
            <JSONPath>$.XMLDATA</JSONPath>
        </Variable>
    </JSONPayload>
    

    This will only work if the Content-Type of your request is application/json (if you want to force it, do an AssignMessage policy just before it and

    <Set>
      <Headers>
        <Header name="Content-type">application/json</Header>
      </Headers>
    </Set>
    

    2) AssignMessage Policy to create new POST payload

    Now that you have a variable named xmldata you can create a new request message with an AssignMessage policy like this:

      <Payload contentType="text/xml">
        &lt;?xml version="1.0" encoding="utf-8"?&gt;
        &lt;GeteBayOfficialTimeRequest xmlns="{xmldata}"&gt;
        &lt;RequesterCredentials&gt;
        &lt;eBayAuthToken&gt;
        </Payload>
      </Set>
    

    Note that you have to escape the < and > in the Payload XML (otherwise Apigee tries to parse it as configuration XML) and note that {xmldata} variable in curlybraces.

    Here are the links to the documentation on Apigee if you need more details about the policies:

    Extract Variables
    http://apigee.com/docs/api-services/content/extract-message-content-using-extractvariables

    Assign Message
    http://apigee.com/docs/api-services/content/extract-message-content-using-extractvariables

    Login or Signup to reply.
  3. If putting XML inside JSON you need to ensure that the XML is escaped and handle it properly. Otherwise you’ll have invalid JSON. If possible, try submitting the JSON request with the data you need and don’t embed the XML. Then as the previous answer suggested:

    1. Extract the required information from the JSON request payload using ExtractVariables policy and <JSONPath>config.
    2. Build the XML request using AssignMessage policy.
    Login or Signup to reply.
  4. Try out DefiantJS (http://defiantjs.com) which has functions that might help you. Among others, it enables you to search JSON structure with XPath query expressions, transform XML to/from JSON structure.

    You can find example of DefiantJS and XPAth examples here:
    http://defiantjs.com/#xpath-evaluator

    Check out this jsfiddle as example;
    http://jsfiddle.net/hbi99/Yc6cc/

    var data = {
        "coupons":{
            "item1":{
                "id":"155",
                "name":"First Item",
                "value":-5199.6
            },
            "item2":{
                "id":"255",
                "name":"Second Item",
                "value":-424.91
            }
        }
    },
    res = JSON.search( data, '//*[id]' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search