0
我想在scala中執行一個curl命令。 curl命令從easypost API獲取運費。下面是代碼我使用:在scala中執行curl命令以獲得easypost的運輸費用API
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)
我越來越:
Shipping data is {"error":{"code":"SHIPMENT.INVALID_PARAMS","message":"Unable to create shipment, one or more parameters were invalid.","errors":[]}}
但在終端正在響應,如:
{"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"}
我在這裏幹什麼什麼了嗎?請建議。
您需要使用double-exclamation-mark運算符('!!')來獲取命令的輸出作爲返回值。 –
爲什麼不使用HTTP庫而不是系統進程?否則,當前代碼將受益於被重構爲shell。 – cchantep
@cchantep我嘗試使用scalaj庫來發送HTTP POST請求。但是我怎樣才能發送-d和-u參數?任何幫助將不勝感激。 – Ishan