Reputation: 1887
I am using Java for Posting json payload to Canvas Rest Api using ApacheHttpClient
I am getting error:Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 422.I have referred the following link on stack overflow POST request to REST API with JSON object as payload
My code is:
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();
}
The app token seems to be working .How do I correct this error,Do i need to pass some server certificares
Upvotes: 0
Views: 229
Reputation: 11
It looks like you're not doing anything with the urlParameters
object. I find it unlikely that the API is expecting that JSON object as a URL parameter, and instead should probably be sent with the body of the request. I could be wrong, just pointing out another potential problem with this code.
Based on the response code, it looks like the query object you are POSTing contains a logical error.
Upvotes: 0