Juan Enrique Riquelme
Juan Enrique Riquelme

Reputation: 415

How can I add a token to a HttpPost Json message

I have this method to send a Json message:

public static void sendRequestPost(JSONObject json) throws IOException, JSONException {

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    try {

        HttpPost request = new HttpPost(Config.urlJSON);
        StringEntity params = new StringEntity(json.toString());
        request.addHeader("Authorization", "Basic " + getBasicAuthenticationEncoding());
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        Header[] headers = request.getAllHeaders();

        String headerFull = "";

        for (int i = 0; i < headers.length; i++) {
            headerFull += headers[i] + " ";
        }

        Log.debug(headerFull);

        int statusCode = httpClient.execute(request).getStatusLine().getStatusCode();

        Log.debug("[STATUS:" + statusCode + "]");

    } catch (Exception ex) {
        Log.debug(ex.toString());
    } finally {
        httpClient.close();
    }
}

I have no problems with this method but now I need to send a token instead the basic authentification.

I tried this line by a curl command without problems:

curl -X POST -H 'Content-Type: application/json' -d '{"pm25": 35, "timestamp": 147805158}' https://url.com/?access-token={Yoq3UGQqDKP4D1L3Y6xIYp-Lb6fyvavpF3Lm-8cD}

And I get a correct response but I couldn't make it work on java I'm just getting a 401 code in return, this is what I have tried in java:

public static void sendRequestPostRenam(JSONObject json) throws IOException, JSONException {

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        try {

            HttpPost request = new HttpPost(Config.urlJSON);
            StringEntity params = new StringEntity(json.toString());
            request.addHeader("Authorization", "Token " + Config.renamToken + "");
            request.setEntity(params);

            int statusCode = httpClient.execute(request).getStatusLine().getStatusCode();

            Log.debug("[STATUS:" + statusCode + "]");

        } catch (Exception ex) {
            Log.debug(ex.toString());
        } finally {
            httpClient.close();
        }
    }

I have tried using the following but without luck:

EDIT:

I haved change the code to this and I'm not getting the 401 code.

public static void sendRequestPostRenam(JSONObject json) throws IOException, JSONException {

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    try {

        URIBuilder builder = new URIBuilder(Config.urlJSON).addParameter("access-token", Config.renamToken);
        HttpPost request = new HttpPost(builder.build());

        StringEntity params = new StringEntity(json.toString());
        request.setEntity(params);

        CloseableHttpResponse response = httpClient.execute(request);
        String content = EntityUtils.toString(response.getEntity());
        Log.debug(content);
        int statusCode = response.getStatusLine().getStatusCode();

        Log.debug("[STATUS:" + statusCode + "]");

    } catch (Exception ex) {
        Log.debug(ex.toString());
    } finally {
        httpClient.close();
    }
}

The print of the content says:

{"status":"error","info":{"timestamp":["Timestamp no puede estar vacío."]},"timestamp":1495057833}

It's like the json parameters it's not been asigned to the entity object.

EDIT:

This is my json object:

{"ruido_exterior":0,"co2_exterior":0,"humedad_interior":0,"ruido_interior":0,"temperatura_exterior":0,"co_interior":0,"co2_interior":0,"co_exterior":0,"temperatura_interior":0,"pm_25":8,"pm_10":10,"humedad_exterior":0,"timestamp":1494978084000}

Upvotes: 1

Views: 2115

Answers (1)

cypher
cypher

Reputation: 6992

Well seeing that in the cURL request, the token is added to URL and not the Authorization header, the same should be in Java.

  URIBuilder builder = new URIBuilder(config.urlJSON).addParameter("access-token", Config.renamToken);
  HttpPost request = new HttpPost(builder.build());

Upvotes: 1

Related Questions