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.