1

Trying to work with QwintryLogistics API, all docs examples in PHP, I don't know PHP at all, coding on Java. In docs needed request looks like this:

<?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);

coded method in Java:

@Override
public String getCost(double weight, String unitsW, String toPickUp, boolean insurance, double value) throws URISyntaxException, IOException, HttpException {
    String url = BASE_URL + "/api/cost/";
    CloseableHttpClient client = HttpClients.createDefault();
    URIBuilder builder = new URIBuilder(url);
    builder
            .addParameter("weight", String.valueOf(weight))
            .addParameter("delivery_pickup", toPickUp)
            .addParameter("insurance", String.valueOf(insurance))
            .addParameter("items_value", String.valueOf(value))
            .addParameter("retail_pricing", String.valueOf(RETAIL_PRICING));

    HttpUriRequest request = new HttpPost(builder.build());
    request.addHeader("User-Agent", USER_AGENT);
    request.addHeader("Authorization", "Bearer " + API_KEY);

    HttpResponse response = client.execute(request);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    return result.toString();
}

But constantly getting response:

{"success":false,"errorCode":0,"errorMessage":"params are empty"}

I think problem is in setting parameters for request. Help me please translate structure of parameters of request in Java terms.

zzheads
  • 1,368
  • 5
  • 28
  • 57

1 Answers1

1

Your PHP code data parameters aren't path params, but request params. You have to write them into the request body, instead of appending them into URL.

You can see how to do it in related question: Java - sending HTTP parameters via POST method easily

However, I would look at higher level REST service clients, like CXF or Jersey.

Community
  • 1
  • 1
MGorgon
  • 2,547
  • 23
  • 41
  • Thanks, started using Unirest library, looks light and clear, but no success still... – zzheads Sep 29 '16 at 23:50
  • Structure of body of request is unclear for me, since I don't know PHP at all. How I can make array of parameters with String as counter? (params[weight], params[items_value],... etc) – zzheads Sep 29 '16 at 23:53
  • Finally made it! There were errors in API documentation, some parameters had wrong names. Now everything works! – zzheads Sep 30 '16 at 00:17