使用ApacheHttpClient的POST REST API

时间:2016-05-06 18:36:29

标签: java json rest post apache-httpclient-4.x

我正在使用Java将json有效负载发布到使用ApacheHttpClient的Canvas Rest Api

我收到错误:线程“main”中的异常java.lang.RuntimeException:失败:HTTP错误代码:422。我在堆栈溢出POST request to REST API with JSON object as payload上引用了以下链接

我的代码是:

     try{

         httpClient = HttpClients.createDefault();
            httpPost = new HttpPost("https://canvas.instructure.com/api/v1/courses/10300000000000133/assignments/10300000000000860");

            List<NameValuePair> headers = new ArrayList<NameValuePair>(); //ArrayList to store header parameters
            List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); //ArrayList to store URL parameters
            urlParameters.add(new BasicNameValuePair("query","{\"id\":10300000000000860,\"description\":\"\",\"due_at\":\"2017-04-19T03:59:59Z\",\"unlock_at\":\"2016-04-13T04:00:00Z\",\"lock_at\":\"2016-04-22T03:59:00Z\",\"points_possible\":3,\"grading_type\":\"points\",\"assignment_group_id\":10300000000000297,\"grading_standard_id\":null,\"created_at\":\"2014-06-21T13:50:05Z\",\"updated_at\":\"2016-05-05T23:53:35Z\",\"peer_reviews\":false,\"automatic_peer_reviews\":false,\"position\":1,\"grade_group_students_individually\":null,\"anonymous_peer_reviews\":null,\"group_category_id\":null,\"post_to_sis\":null,\"moderated_grading\":null,\"course_id\":10300000000000133,\"name\":\"Units and Dimensions new\",\"submission_types\":[\"online_quiz\"],\"has_submitted_submissions\":false,\"muted\":false,\"html_url\":\"https://canvas.instructure.com/courses/10300000000000133/assignments/1030~860\",\"has_overrides\":false,\"needs_grading_count\":0,\"integration_id\":null,\"integration_data\":{},\"quiz_id\":10300000000000539,\"anonymous_submissions\":false,\"published\":true,\"unpublishable\":true,\"only_visible_to_overrides\":false,\"locked_for_user\":false,\"submissions_download_url\":\"https://canvas.instructure.com/courses/1030~133/quizzes/1030~539/submissions?zip=1\"}"));
            headers.add(new BasicNameValuePair("app-token", "1030~SAD..."));
            headers.add(new BasicNameValuePair("Accept", "application/json, text/javascript, */*; q=0.01"));
            headers.add(new BasicNameValuePair("X-Requested-With", "XMLHttpRequest"));

            for (NameValuePair h : headers)
            {
                httpPost.addHeader(h.getName(), h.getValue());
            }

            response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatusLine().getStatusCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (response.getEntity().getContent())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }
        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

app令牌似乎正在运行。如何更正此错误,我是否需要传递一些服务器证书

1 个答案:

答案 0 :(得分:0)

看起来你没有对urlParameters对象做任何事情。我发现API不太可能期望JSON对象作为URL参数,而应该与请求的主体一起发送。我可能错了,只是指出了这个代码的另一个潜在问题。

基于response code,看起来你正在POST的查询对象包含逻辑错误。