Java
Chilkat2 Java
Shippo Create an Order
See more Shippo Examples
Demonstrates how to create an order with all the information about your order by sending a POST request to the orders endpoint.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
// Implements the following CURL command:
// curl https://api.goshippo.com/orders/ \
// -H "Authorization: ShippoToken <API_Token>" \
// -H "Content-Type: application/json" \
// -d '{
// "to_address": {
// "city": "San Francisco",
// "company": "Shippo",
// "country": "US",
// "email": "shippotle@goshippo.com",
// "name": "Mr Hippo",
// "phone": "15553419393",
// "state": "CA",
// "street1": "215 Clayton St.",
// "zip": "94117"
// },
// "line_items": [
// {
// "quantity": 1,
// "sku": "HM-123",
// "title": "Hippo Magazines",
// "total_price": "12.10",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }
// ],
// "placed_at": "2016-09-23T01:28:12Z",
// "order_number": "#1068",
// "order_status": "PAID",
// "shipping_cost": "12.83",
// "shipping_cost_currency": "USD",
// "shipping_method": "USPS First Class Package",
// "subtotal_price": "12.10",
// "total_price": "24.93",
// "total_tax": "0.00",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "to_address": {
// "city": "San Francisco",
// "company": "Shippo",
// "country": "US",
// "email": "shippotle@goshippo.com",
// "name": "Mr Hippo",
// "phone": "15553419393",
// "state": "CA",
// "street1": "215 Clayton St.",
// "zip": "94117"
// },
// "line_items": [
// {
// "quantity": 1,
// "sku": "HM-123",
// "title": "Hippo Magazines",
// "total_price": "12.10",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }
// ],
// "placed_at": "2016-09-23T01:28:12Z",
// "order_number": "#1068",
// "order_status": "PAID",
// "shipping_cost": "12.83",
// "shipping_cost_currency": "USD",
// "shipping_method": "USPS First Class Package",
// "subtotal_price": "12.10",
// "total_price": "24.93",
// "total_tax": "0.00",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }
JsonObject json = new JsonObject();
json.updateString("to_address.city", "San Francisco");
json.updateString("to_address.company", "Shippo");
json.updateString("to_address.country", "US");
json.updateString("to_address.email", "shippotle@goshippo.com");
json.updateString("to_address.name", "Mr Hippo");
json.updateString("to_address.phone", "15553419393");
json.updateString("to_address.state", "CA");
json.updateString("to_address.street1", "215 Clayton St.");
json.updateString("to_address.zip", "94117");
json.updateInt("line_items[0].quantity", 1);
json.updateString("line_items[0].sku", "HM-123");
json.updateString("line_items[0].title", "Hippo Magazines");
json.updateString("line_items[0].total_price", "12.10");
json.updateString("line_items[0].currency", "USD");
json.updateString("line_items[0].weight", "0.40");
json.updateString("line_items[0].weight_unit", "lb");
json.updateString("placed_at", "2016-09-23T01:28:12Z");
json.updateString("order_number", "#1068");
json.updateString("order_status", "PAID");
json.updateString("shipping_cost", "12.83");
json.updateString("shipping_cost_currency", "USD");
json.updateString("shipping_method", "USPS First Class Package");
json.updateString("subtotal_price", "12.10");
json.updateString("total_price", "24.93");
json.updateString("total_tax", "0.00");
json.updateString("currency", "USD");
json.updateString("weight", "0.40");
json.updateString("weight_unit", "lb");
http.setRequestHeader("Authorization", "ShippoToken <API_Token>");
http.setRequestHeader("Content-Type", "application/json");
HttpResponse resp = new HttpResponse();
try {
http.httpJson("POST", "https://api.goshippo.com/orders/", json, "application/json", resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
StringBuilder sbResponseBody = new StringBuilder();
resp.getBodySb(sbResponseBody);
JsonObject jResp = new JsonObject();
jResp.loadSb(sbResponseBody);
jResp.setEmitCompact(false);
System.out.println("Response Body:");
System.out.println(jResp.emit());
int respStatusCode = resp.getStatusCode();
System.out.println("Response Status Code = " + respStatusCode);
if (respStatusCode >= 400) {
System.out.println("Response Header:");
System.out.println(resp.getHeader());
System.out.println("Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {}
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
}
}