Dart
Dart
Shippo Adding Metadata
See more Shippo Examples
Demonstrates how to add metadata to the tracking request through a POST request.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final http = CkHttp();
// Implements the following CURL command:
// curl https://api.goshippo.com/tracks/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -d carrier="shippo" \
// -d tracking_number="SHIPPO_TRANSIT" \
// -d metadata="Order 000123"
final req = CkHttpRequest();
req.httpVerb = 'POST';
req.path = '/tracks/';
req.contentType = 'application/x-www-form-urlencoded';
req.addParam('carrier', 'shippo');
req.addParam('tracking_number', 'SHIPPO_TRANSIT');
req.addParam('metadata', 'Order 000123');
req.addHeader('Authorization', 'ShippoToken <API_TOKEN>');
final resp = CkHttpResponse();
try {
http.httpReq('https://api.goshippo.com/tracks/', req, resp);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final sbResponseBody = CkStringBuilder();
resp.getBodySb(sbResponseBody);
final jResp = CkJsonObject();
jResp.loadSb(sbResponseBody);
jResp.emitCompact = false;
print('Response Body:');
print(jResp.emit());
final respStatusCode = resp.statusCode;
print('Response Status Code = $respStatusCode');
if (respStatusCode >= 400) {
print('Response Header:');
print(resp.header);
print('Failed.');
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "messages": [
// ],
// "carrier": "shippo",
// "tracking_number": "SHIPPO_TRANSIT",
// "address_from": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// },
// "address_to": {
// "city": "Chicago",
// "state": "IL",
// "zip": "60611",
// "country": "US"
// },
// "eta": "2019-07-07T17:07:44.989Z",
// "original_eta": "2019-07-07T17:07:44.989Z",
// "servicelevel": {
// "token": "shippo_priority",
// "name": "Priority Mail"
// },
// "metadata": "Shippo test tracking",
// "tracking_status": {
// "object_created": "2019-07-04T17:07:45.003Z",
// "object_updated": null,
// "object_id": "ee35fb56f5d04021b36168abedc04573",
// "status": "TRANSIT",
// "status_details": "Your shipment has departed from the origin.",
// "status_date": "2019-07-03T15:02:45.003Z",
// "substatus": null,
// "location": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// }
// },
// "tracking_history": [
// {
// "object_created": "2019-07-04T17:07:45.005Z",
// "object_updated": null,
// "object_id": "2121a59f53ed42e0ae0436f636179156",
// "status": "UNKNOWN",
// "status_details": "The carrier has received the electronic shipment information.",
// "status_date": "2019-07-02T12:57:45.005Z",
// "substatus": null,
// "location": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// }
// },
// {
// "object_created": "2019-07-04T17:07:45.005Z",
// "object_updated": null,
// "object_id": "06f949db1a8245beaa28df264b368a76",
// "status": "TRANSIT",
// "status_details": "Your shipment has departed from the origin.",
// "status_date": "2019-07-03T15:02:45.005Z",
// "substatus": null,
// "location": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// }
// }
// ],
// "transaction": null,
// "test": true
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var objectCreated = '';
var objectUpdated = '';
var objectId = '';
var status = '';
var statusDetails = '';
var statusDate = '';
var substatus = '';
var locationCity = '';
var locationState = '';
var locationZip = '';
var locationCountry = '';
final carrier = jResp.stringOf('carrier');
final trackingNumber = jResp.stringOf('tracking_number');
final addressFromCity = jResp.stringOf('address_from.city');
final addressFromState = jResp.stringOf('address_from.state');
final addressFromZip = jResp.stringOf('address_from.zip');
final addressFromCountry = jResp.stringOf('address_from.country');
final addressToCity = jResp.stringOf('address_to.city');
final addressToState = jResp.stringOf('address_to.state');
final addressToZip = jResp.stringOf('address_to.zip');
final addressToCountry = jResp.stringOf('address_to.country');
final eta = jResp.stringOf('eta');
final originalEta = jResp.stringOf('original_eta');
final servicelevelToken = jResp.stringOf('servicelevel.token');
final servicelevelName = jResp.stringOf('servicelevel.name');
final metadata = jResp.stringOf('metadata');
final trackingStatusObjectCreated = jResp.stringOf('tracking_status.object_created');
final trackingStatusObjectUpdated = jResp.stringOf('tracking_status.object_updated');
final trackingStatusObjectId = jResp.stringOf('tracking_status.object_id');
final trackingStatusStatus = jResp.stringOf('tracking_status.status');
final trackingStatusStatusDetails = jResp.stringOf('tracking_status.status_details');
final trackingStatusStatusDate = jResp.stringOf('tracking_status.status_date');
final trackingStatusSubstatus = jResp.stringOf('tracking_status.substatus');
final trackingStatusLocationCity = jResp.stringOf('tracking_status.location.city');
final trackingStatusLocationState = jResp.stringOf('tracking_status.location.state');
final trackingStatusLocationZip = jResp.stringOf('tracking_status.location.zip');
final trackingStatusLocationCountry = jResp.stringOf('tracking_status.location.country');
final transaction = jResp.stringOf('transaction');
var i = 0;
var countI = jResp.sizeOfArray('messages');
while (i < countI) {
jResp.i = i;
i++;
}
i = 0;
countI = jResp.sizeOfArray('tracking_history');
while (i < countI) {
jResp.i = i;
objectCreated = jResp.stringOf('tracking_history[i].object_created');
objectUpdated = jResp.stringOf('tracking_history[i].object_updated');
objectId = jResp.stringOf('tracking_history[i].object_id');
status = jResp.stringOf('tracking_history[i].status');
statusDetails = jResp.stringOf('tracking_history[i].status_details');
statusDate = jResp.stringOf('tracking_history[i].status_date');
substatus = jResp.stringOf('tracking_history[i].substatus');
locationCity = jResp.stringOf('tracking_history[i].location.city');
locationState = jResp.stringOf('tracking_history[i].location.state');
locationZip = jResp.stringOf('tracking_history[i].location.zip');
locationCountry = jResp.stringOf('tracking_history[i].location.country');
i++;
}
}