scalacurleasypost

Execute curl command in scala to get shipping charges from easypost API


I am trying execute a curl command in scala. The curl command fetched the shipping charges from easypost API. Below is the code I am using :

 import sys.process._
 val data="curl -X POST https://api.easypost.com/v2/shipments -u <Easypost Test API Key>: -d 'shipment[to_address][zip]=90277' -d 'shipment[from_address][zip]=94104' -d 'shipment[parcel][length]=20.2' -d 'shipment[parcel][width]=10.9' -d 'shipment[parcel][height]=5' -d 'shipment[parcel][weight]=65.9'".!!
 println("Shipping data is "+data)

I am getting :

Shipping data is {"error":{"code":"SHIPMENT.INVALID_PARAMS","message":"Unable to create shipment, one or more parameters were invalid.","errors":[]}}

But in terminal it is responding like :

{"created_at":"2017-03-03T05:31:03Z","is_return":false,"messages":[],"mode":"test","options":{"currency":"USD","label_date":null,"date_advance":0},"reference":null,"status":"unknown","tracking_code":null,"updated_at":"2017-03-03T05:31:03Z","batch_id":null,"batch_status":null,"batch_message":null,"customs_info":null,"from_address":{"id":"adr_382aaa644ccb4ecfb3f14db65275dc47","object":"Address","created_at":"2017-03-03T05:31:03Z","updated_at":"2017-03-03T05:31:03Z","name":null,"company":null,"street1":null,"street2":null,"city":null,"state":null,"zip":"94104","country":"US","phone":null,"email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"insurance":null,"order_id":null,"parcel":{"id":"prcl_adf352eee75d43339279f959b8cd1118","object":"Parcel","created_at":"2017-03-03T05:31:03Z","updated_at":"2017-03-03T05:31:03Z","length":20.2,"width":10.9,"height":5.0,"predefined_package":null,"weight":65.9,"mode":"test"},"postage_label":null,"rates":[{"id":"rate_9799f33dbc99420abeba4101d6a0d31f","object":"Rate","created_at":"2017-03-03T05:31:03Z","updated_at":"2017-03-03T05:31:03Z","mode":"test","service":"Express","carrier":"USPS","rate":"37.08","currency":"USD","retail_rate":"41.80","retail_currency":"USD","list_rate":"37.08","list_currency":"USD","delivery_days":null,"delivery_date":null,"delivery_date_guaranteed":false,"est_delivery_days":null,"shipment_id":"shp_54916a9085114979a300c0ba7b10efd7","carrier_account_id":"ca_bba7f2862b2e4a6aa682dcf5eeb0de38"},{"id":"rate_df1211dd22ab4aefa83cc34e206acb9c","object":"Rate","created_at":"2017-03-03T05:31:03Z","updated_at":"2017-03-03T05:31:03Z","mode":"test","service":"Priority","carrier":"USPS","rate":"8.91","currency":"USD","retail_rate":"11.95","retail_currency":"USD","list_rate":"9.19","list_currency":"USD","delivery_days":2,"delivery_date":null,"delivery_date_guaranteed":false,"est_delivery_days":2,"shipment_id":"shp_54916a9085114979a300c0ba7b10efd7","carrier_account_id":"ca_bba7f2862b2e4a6aa682dcf5eeb0de38"},{"id":"rate_37f9ba8a68304222b02c15019a02918a","object":"Rate","created_at":"2017-03-03T05:31:03Z","updated_at":"2017-03-03T05:31:03Z","mode":"test","service":"ParcelSelect","carrier":"USPS","rate":"9.19","currency":"USD","retail_rate":"9.19","retail_currency":"USD","list_rate":"9.19","list_currency":"USD","delivery_days":5,"delivery_date":null,"delivery_date_guaranteed":false,"est_delivery_days":5,"shipment_id":"shp_54916a9085114979a300c0ba7b10efd7","carrier_account_id":"ca_bba7f2862b2e4a6aa682dcf5eeb0de38"}],"refund_status":null,"scan_form":null,"selected_rate":null,"tracker":null,"to_address":{"id":"adr_9aa339f8acd244059e5ffb775c541dba","object":"Address","created_at":"2017-03-03T05:31:03Z","updated_at":"2017-03-03T05:31:03Z","name":null,"company":null,"street1":null,"street2":null,"city":null,"state":null,"zip":"90277","country":"US","phone":null,"email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"usps_zone":4,"return_address":{"id":"adr_382aaa644ccb4ecfb3f14db65275dc47","object":"Address","created_at":"2017-03-03T05:31:03Z","updated_at":"2017-03-03T05:31:03Z","name":null,"company":null,"street1":null,"street2":null,"city":null,"state":null,"zip":"94104","country":"US","phone":null,"email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"buyer_address":{"id":"adr_9aa339f8acd244059e5ffb775c541dba","object":"Address","created_at":"2017-03-03T05:31:03Z","updated_at":"2017-03-03T05:31:03Z","name":null,"company":null,"street1":null,"street2":null,"city":null,"state":null,"zip":"90277","country":"US","phone":null,"email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"forms":[],"fees":[],"id":"shp_54916a9085114979a300c0ba7b10efd7","object":"Shipment"}

Am I doing something wrong here ? Please suggest.


Solution

  • It looks like curl is successfully contacting the service, but it is rejecting your request.

    Are you sure you are using the exact same arguments on the command-line as you are in the scala version? The trailing ":" on the API key looks suspicious to me.

    I wonder if this is a shell escaping problem. Maybe try using the Seq[String] form of ProcessBuilder, to avoid any shell escaping?

    import sys.process._
    val data = List("curl", "-X", "POST", "https://api.easypost.com/v2/shipments", "-u", "<Easypost Test API Key>:", "-d", "shipment[to_address][zip]=90277", "-d", "shipment[from_address][zip]=94104", "-d", "shipment[parcel][length]=20.2", "-d", "shipment[parcel][width]=10.9", "-d", "shipment[parcel][height]=5", "-d", "shipment[parcel][weight]=65.9").!!     
    println("Shipping data is " + data)