0

I can use the REST API using cURL like in this answer. Now I want to "translate" it to Java code using the Apache Commons HTTPComponents library. I'm only having trouble adding the API key authorization header:

HttpPost request = new HttpPost(DE_COMPILATION_URL);
byte[] encodedAuth = Base64.encodeBase64(API_KEY.getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + new String(encodedAuth);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
request.setHeader("mode", "raw");
request.setHeader("input", "@" + machineCodeFile.toString() + ";filename=" + machineCodeFile.getName());
request.setHeader("architecture", "powerpc");
request.setHeader("endian", "big");
request.setHeader("raw_entry_point", "0x0");
request.setHeader("raw_section_vma", "0x0");
request.setHeader("target_language", "c");

HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);

This prints:

{
    "code": 401,
    "description": "API key authorization failed (missing or invalid API key).",
    "message": "Unauthorized by API Key"
}

I tried different pieces of authorization code but none worked. What could be the problem? The API key is correct and works with cURL.

1 Answer 1

1

Nevermind, I forgot a colon at the end. It's mandatory to include in the base64 encoding:

HttpPost request = new HttpPost(DE_COMPILATION_URL);
byte[] encodedAuth = Base64.encodeBase64((API_KEY + ":").getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + new String(encodedAuth);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.