我正在尝试在scala中执行curl命令。 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"}
我在这里做错了吗?请建议。
答案 0 :(得分:1)
看起来curl
已成功联系该服务,但它拒绝了您的请求。
您确定在命令行中使用与scala版本中完全相同的参数吗? API密钥上的尾随“:”看起来很可疑。
我想知道这是否是一个shell转义问题。也许尝试使用Seq[String]
形式的ProcessBuilder
,以避免任何shell转义?
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)