2016-09-29 55 views
1

爲與QwintryLogistics API(http://logistics.qwintry.com)連接提供服務,在Java + Unirest庫上編碼,但所有API文檔都在PHP中,因此請求體的結構對我來說不清楚。關於POST請求的文檔 部分:無法配置Unirest請求體

<?php 
    define('SITE_URL', 'logistics.qwintry.com'); 
    define('API_KEY', 'YOUR_API_KEY'); //don't forget to set your key! 
    $url = 'http://'. SITE_URL .'/api/cost'; 
    $data = array ( 
     'params' => array(
      'weight' => 5, // in lb 
      'delivery_pickup' => 'msk_1', // full list of pickup points can be retrieved from /api/locations-list 
      'insurance' => true, 
      'items_value' => 500, // declaration items total cost in USD 
      'retail_pricing' => true // retail/wholesale pricing? 
     ), 
    ); 
    $data_string = http_build_query($data); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. API_KEY)); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    $response = curl_exec($ch); 
    curl_close($ch); 
    var_dump($response); 

所以我需要幫助來配置我Unirest POST請求。 任何幫助將不勝感激。

回答

0

它製造這樣的:

Map<String, Object> params = new HashMap<>(); 
    params.put("params[weight_kg]", weight); 
    params.put("params[dimensions_cm]", dimensions); 
    params.put("params[delivery_pickup]", toPickup); 
    params.put("params[insurance]", insurance); 
    params.put("params[items_value]", value); 
    params.put("params[retail_pricing]", RETAIL_PRICING); 

    String url = BASE_URL+"/api/cost"; 
    HttpResponse<JsonNode> jsonResponse = Unirest.post(url).fields(params).asJson(); 

作品完美:)