1

I want to send a POST Request to the Octoprint API via the Apache HttpClient, something like shown here: http://docs.octoprint.org/en/master/api/job.html#issue-a-job-command (e.g. to start a job). I've read the docs of both, but still getting "Bad Request" as answer.

Tried several other Post Request, but never getting something else. Guess I'm writing the request wrong somehow.

CloseableHttpClient posterClient = HttpClients.createDefault();
        HttpPost post = new HttpPost("http://localhost:5000/api/job");
        post.setHeader("Host", "http://localhost:5000");
        post.setHeader("Content-type", "application/json");

        post.setHeader("X-Api-Key", "020368233D624EEE8029991AE80A729B");

        List<NameValuePair> content = new ArrayList<NameValuePair>();
        content.add(new BasicNameValuePair("command", "start"));



        post.setEntity(new UrlEncodedFormEntity(content));
        CloseableHttpResponse answer = posterClient.execute(post);

        System.out.println(answer.getStatusLine());
Flontis
  • 307
  • 1
  • 4
  • 13

1 Answers1

0

Likely the content type is wrong. As per documentation here, expect bodies to be in JSON formart. Your code on the other hand uses application/x-www-form-urlencoded as per this piece of code post.setEntity(new UrlEncodedFormEntity(content));

Quick fix, make the below changes and try it out:

    String json= "{\"command\":\"start\"}";
    
    //This will change change you BasicNameValuePair to an Entity with the correct Content Type
    StringEntity entity = new StringEntity(json,ContentType.APPLICATION_JSON);
    //Now you just set it to the body of your post
    post.setEntity(entity);

You will likely want to review how you create the content of your post. The above is just so you check if the issue is indeed related to the content type.

Let us know of the outcome.

Community
  • 1
  • 1
Estevao Santiago
  • 793
  • 2
  • 7
  • 30
  • Add this line after entity declaration: `entity.setContentEncoding("application/json");` if it still doesn't work, I will take a second look on the docs again. – Estevao Santiago Jul 25 '19 at 18:12
  • Jep, still doesn't work... looks like that now: content.add(new BasicNameValuePair("command", "start")); StringEntity entity = new StringEntity(content.toString(),ContentType.APPLICATION_JSON); entity.setContentEncoding("application/json"); post.setEntity(entity); CloseableHttpResponse answer = posterClient.execute(post); – Flontis Jul 25 '19 at 18:15
  • OK, in won't work likely because of how we are trying to create the json. Your `content` variable prints as `[command=start]` and that is indeed wrong. You can view it by using `System.out.println(EntityUtils.toString(entity));`. You will need to create your JSON using a library for that. Which json library you have on your project? – Estevao Santiago Jul 25 '19 at 18:27
  • Im using Jackson, but if you can suggest a better one, feel free. Don't have much knowledge anyway and haven't written much JSON-code yet in this project. – Flontis Jul 25 '19 at 18:31
  • Since we are just trying... take a look at the updated code, I wrote a json manually. Not really a long term solution, but it will tell us if we are going the right way. Try it and let me know if the error persists. – Estevao Santiago Jul 25 '19 at 18:39
  • Works now! Getting back the expected "204 NO CONTENT". Thank you very much! – Flontis Jul 25 '19 at 18:43