Page 1 of 1
Rest API Call for Aftership
Posted: Wed Dec 19, 2018 12:37 am
by ace16vitamine
Dear all,
I need to connect the aftership API to receive a tracking status for my Christmas gifts
https://docs.aftership.com/api/4/tracki ... -trackings
How can I convert this curl command to Lice Code?
Code: Select all
curl -X POST \
https://api.aftership.com/v4/trackings \
-H 'Content-Type: application/json' \
-H 'Postman-Token: ABCDEF' \
-H 'aftership-api-key: GHIJKLM' \
-H 'cache-control: no-cache' \
-d '{
"tracking": {
"tracking_number": "123456789"
}
}'
Thanks
Stef
Re: Rest API Call for Aftership
Posted: Wed Dec 19, 2018 8:31 am
by bangkok
ace16vitamine wrote: ↑Wed Dec 19, 2018 12:37 am
How can I convert this curl command to Lice Code?
very simple.
first you set the proper "header"
second you issue a simple POST.
2 ways to do that :
old school
and new school (LiveCode with TSNET)
old school (but still working with any version of LiveCode) :
Code: Select all
set the httpheaders to yourheader
put "https://api.aftership.com/v4/trackings" into tURL
post tPostData to url tURL
put it into tResult
new school :
Code: Select all
put yourheader into tRequestHeaders
put "https://api.aftership.com/v4/trackings" into tURL
put tsNetPostSync(tUrl, tRequestHeaders, tPostData, tResponseHeaders, tResult, tBytes, tSettings) into tData
Re: Rest API Call for Aftership
Posted: Wed Dec 19, 2018 12:21 pm
by bogs
bangkok wrote: ↑Wed Dec 19, 2018 8:31 am
old school (but still working with any version of LiveCode)
Thank you, bangkok, that is very helpful to know, and I hadn't come across it yet

Re: Rest API Call for Aftership
Posted: Wed Dec 19, 2018 3:30 pm
by ace16vitamine
Thanks!
I did it, but I have some problems to convert into JSON Format:
Code: Select all
put "tracking_number: " & quote & "ABCD" & quote into myData["tracking"]
put jsonExport(myData) into tPostData
set the httpheaders to "aftership-api-key: 12345"&CR&"Content-Type: application/json"
put "https://api.aftership.com/v4/trackings" into tURL
post tPostData to url tURL
put it into tResult
Error Message:
{"meta":{"code":4007,"message":"`tracking_number` is required.","type":"BadRequest"},"data":
{"tracking":"tracking_number: \"ABCD\""}}
It seems that the API is expecting this JSON Format:
Code: Select all
{
"tracking": {
"tracking_number": "ABCD"
}
}'
How can I convert this?
Stef
Re: Rest API Call for Aftership
Posted: Wed Dec 19, 2018 5:44 pm
by MaxV
Re: Rest API Call for Aftership
Posted: Wed Dec 19, 2018 11:03 pm
by bwmilby
Try...
put "ABCD" into myData["tracking"]["tracking_number"]
Site is expecting a nested dictionary based on the initial example.
Since the full JSON is provided in the curl example, you could just use that and merge in your tracking number.
Re: Rest API Call for Aftership
Posted: Wed Dec 19, 2018 11:17 pm
by ace16vitamine
put "ABCD" into myData["tracking"]["tracking_number"]
put jsonExport(myData) into tPostData
Thanks it!
Thank you for your support!
Re: Rest API Call for Aftership
Posted: Thu Dec 20, 2018 2:39 am
by ace16vitamine
But....
know I have another question
The answer is also in JSON Format:
{"meta":{"code":201},"data":{"tracking":{"id":"XYZ","created_at":"2018-12-20T00:38:01+00:00","updated_at":"2018-12-20T00:38:01+00:00","last_updated_at":"2018-12-20T00:38:01+00:00","tracking_number":"ABCD","slug":"dhl-germany","active":true,"android":[],"custom_fields":null,"customer_name":null,"delivery_time":0,"destination_country_iso3":null,"courier_destination_country_iso3":null,"emails":[],"expected_delivery":null,"ios":[],"note":null,"order_id":"AUM11231","order_id_path":null,"origin_country_iso3":null,"shipment_package_count":0,"shipment_pickup_date":null,"shipment_delivery_date":null,"shipment_type":null,"shipment_weight":null,"shipment_weight_unit":null,"signed_by":null,"smses":[],"source":"api","tag":"Pending","subtag":"Pending_001","subtag_message":"Pending","title":"ABCD","tracked_count":0,"last_mile_tracking_supported":null,"language":"de","unique_token":"deprecated","checkpoints":[],"subscribed_smses":[],"subscribed_emails":[],"return_to_sender":false,"tracking_account_number":null,"tracking_origin_country":null,"tracking_destination_country":null,"tracking_key":null,"tracking_postal_code":null,"tracking_ship_date":null,"tracking_state":null}}}
What can I do to get the variable from Tracking ID (XYZ)?
My Idea was
Code: Select all
put jsonImport(vorconvert) into temp --put the answer into temp
--put the keys of temp (Shows: META and DATA)
put temp["data"]["id"]
But I did not received anything from the temp["data"]["id"] array. What is wrong?
Thanks
Stef
Re: Rest API Call for Aftership
Posted: Thu Dec 20, 2018 7:41 am
by bwmilby
Code: Select all
put temp["data"]["tracking"]["id"]
All of the keys are one more level down in the array. Also helpful to insert a breakpoint after the import to examine the actual array.
Re: Rest API Call for Aftership
Posted: Thu Dec 20, 2018 3:01 pm
by ace16vitamine
Thanks a lot, Brian! You are right, the array is now working.
Now I need to work with a GET query:
Code: Select all
curl -X GET \
https://api.aftership.com/v4/last_checkpoint/12345 \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 6789' \
-H 'aftership-api-key: 101112' \
-H 'cache-control: no-cache'
Before it was a POST. Any Idea how to create the code in LC?
Thanks
Stefan
Re: Rest API Call for Aftership
Posted: Thu Dec 20, 2018 3:52 pm
by bwmilby
After setting headers as before, should just be:
Re: Rest API Call for Aftership
Posted: Wed Mar 18, 2020 4:14 am
by keliko
bangkok wrote: ↑Wed Dec 19, 2018 8:31 am
ace16vitamine wrote: ↑Wed Dec 19, 2018 12:37 am
How can I convert this curl command to Lice Code?
very simple.
first you set the proper "header"
second you issue a simple POST.
2 ways to do that :
old school
and new school (LiveCode with TSNET)
old school (but still working with any version of LiveCode) :
Code: Select all
set the httpheaders to yourheader
put "https://api.aftership.com/v4/trackings" into tURL
post tPostData to url tURL
put it into tResult
new school :
Code: Select all
put yourheader into tRequestHeaders
put "https://api.aftership.com/v4/trackings" into tURL
put tsNetPostSync(tUrl, tRequestHeaders, tPostData, tResponseHeaders, tResult, tBytes, tSettings) into tData
what is the difference between tsnet and old school??
Re: Rest API Call for Aftership
Posted: Wed Mar 18, 2020 6:00 am
by bangkok
keliko wrote: ↑Wed Mar 18, 2020 4:14 am
what is the difference between tsnet and old school??
The doc : "tsNet is a LiveCode external that replaces and enhances the networking capabilities of the platform."
https://livecode.com/tsnet-new-networki ... -livecode/
tsNet comes into several "flavors" (with different capabilities) : normal, Indy, Business.
https://www.techstrategies.com.au/tsnet ... -external/
When i was talking about "old school" i was talking about the names of the commands.
The old ones still work, but are "handled" by tsNet, from a technical point of view.
Re: Rest API Call for Aftership
Posted: Wed Mar 18, 2020 6:03 am
by keliko
Thanks bangkok.
