我想通过 Apache HttpClient 向 Octoprint API 发送一个 POST 请求,如下所示:http://docs.octoprint.org/en/master/api/job.html#issue-a-job-command (例如开始工作)。我已经阅读了两者的文档,但仍然得到“错误请求”作为答案。
尝试了其他几个发布请求,但从未得到其他东西。猜猜我以某种方式写错了请求。
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());