Help with API

I have an api from a supplier which I need to get the access token.
I can get it… but I can’t get it to store in set value.
This is the data returned:

{“api1”:{“status”:200,“headers”:{“0”:“HTTP/1.1 200 OK”,“Date”:“Fri, 10 Apr 2020 04:20:18 GMT”,“Server”:“Jetty(7.6.21.v20160908)”,“Strict-Transport-Security”:“max-age=15768000”,“X-Frame-Options”:“SAMEORIGIN”,“X-XSS-Protection”:“1; mode=block”,“X-Content-Type-Options”:“nosniff”,“Content-Type”:“text/xml;charset=UTF-8”,“Cache-Control”:“max-age=0, private, must-revalidate”,“X-Request-Id”:“c071401f-5578-4ca6-9bff-324f41e60b3c”,“X-Runtime”:“0.094000”,“Accept-Ranges”:“none”,“Vary”:“Accept-Encoding”,“Content-Encoding”:“gzip”,“Transfer-Encoding”:“chunked”},“data”:"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n true</result>\n 9f8f50b2-aa2c-4185-b098-5b3b20c7a44e</token>\n 14:20 11-Apr-2020</expiry>\n</authentication>\n"}}

I need to save the part into a value. I have tried {{api1.data.token}} but this is not working
It seems to be returning it in XML format

Anyone any ideas???

Hi @gunnery,
It is returning the token as an XML string. The quickest, and slightly crude, way of dealing with it would be to manipulate the string to extract just the token. You should be able to use:
{{api1.data.split("</result>\n ")[1].split("</token>")[0]}}
It will return everything after the result tag, newline character and space. It then returns the string before the token tag leaving you with just the token. If the provider change or reorder their XML, you may have to adjust the code.

I’ve added that to the Set Value and still getting nothing

This is the link to the api
http://pos.thegunnery.co/dmxConnect/api/Import-Stock/Winchester/api.php

Can you post a screenshot of your server action steps please?

1 Like

1 Like

Is there always a space at the start of the token? I’m wondering whether the escaped output may be causing the split lookup string to not match as what I posted was based on that escaped version. You could split on result> (rather than </result>/n ) before then splitting on the space. Something like:
{{api1.data.split("result>")[1].split(" ")[1].split("</token")[0]}}

It seems as though any PHP function options may be limited as the XML string doesn’t appear to be correctly structured. The fields usually fall inside a single tab e.g. <records></records> in the following
<records> <field1></field1> <field2></field2> <record1> <recordfield1></recordfield1> <recordfield2></recordfield2> </record1> <record2> <recordfield1></recordfield1> <recordfield2></recordfield2> </record2> </records>

Unless a field is empty it should also have both opening and closing tags.

These 2 issues, afik, will cause the usual PHP functions to throw errors leaving string manipulation as your only option.

Cheers @bpj for your help but still the same issues.
I think this one will have to go in the too hard basket at the moment.
I’ve flicked an email to the supplier asking if they can supply a JSON API as the XML is outdated.
Going to have to work another way around this at the moment I think.

1 Like