Rest API Call for Aftership

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Rest API Call for Aftership

Post by ace16vitamine » Wed Dec 19, 2018 12:37 am

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

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Rest API Call for Aftership

Post by bangkok » 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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Rest API Call for Aftership

Post by bogs » Wed Dec 19, 2018 12:21 pm

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 :D
Image

ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Re: Rest API Call for Aftership

Post by ace16vitamine » Wed Dec 19, 2018 3:30 pm

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

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Rest API Call for Aftership

Post by MaxV » Wed Dec 19, 2018 5:44 pm

Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

bwmilby
Posts: 438
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: Rest API Call for Aftership

Post by bwmilby » Wed Dec 19, 2018 11:03 pm

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.
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Re: Rest API Call for Aftership

Post by ace16vitamine » Wed Dec 19, 2018 11:17 pm

put "ABCD" into myData["tracking"]["tracking_number"]
put jsonExport(myData) into tPostData

Thanks it!

Thank you for your support!

ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Re: Rest API Call for Aftership

Post by ace16vitamine » Thu Dec 20, 2018 2:39 am

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

bwmilby
Posts: 438
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: Rest API Call for Aftership

Post by bwmilby » Thu Dec 20, 2018 7:41 am

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.
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

ace16vitamine
Posts: 130
Joined: Fri Apr 13, 2018 1:53 pm

Re: Rest API Call for Aftership

Post by ace16vitamine » Thu Dec 20, 2018 3:01 pm

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

bwmilby
Posts: 438
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: Rest API Call for Aftership

Post by bwmilby » Thu Dec 20, 2018 3:52 pm

After setting headers as before, should just be:

Code: Select all

get url tUrl
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

keliko
Posts: 85
Joined: Thu Aug 01, 2019 8:15 am

Re: Rest API Call for Aftership

Post by keliko » Wed Mar 18, 2020 4:14 am

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??

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Rest API Call for Aftership

Post by bangkok » Wed Mar 18, 2020 6:00 am

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.

keliko
Posts: 85
Joined: Thu Aug 01, 2019 8:15 am

Re: Rest API Call for Aftership

Post by keliko » Wed Mar 18, 2020 6:03 am

Thanks bangkok. :D

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”