Canvas API format

161 views
Skip to first unread message

Aurelien Petit

unread,
Oct 14, 2015, 10:55:49 AM10/14/15
to Canvas LMS Users
Hello,
I am pretty new with Canvas LMS, I am use to use Rails and ruby but never in this way, I am trying to use the API to enroll a user from another system.
So I go ahead and build my function
data = {
   enrollment: {
user_id: @check_user_exist.first.id.to_s,
type: "StudentEnrollment",
role_id: 3,
enrollment_state: "invited",
course_section_id: @section.first.id.to_i,
limit_privileges_to_course_section: true,
notify: true,
self_enrolled: false,
     }
}
if @check_enrolled_exist.empty?
Net::HTTP.post_form(URI.parse(ENV["URL"] + '/api/v1/sections/'+ @section.first.id.to_s + '/enrollments?access_token=' + ENV["ACCESSTOKEN"]), data)        
else
        puts 'User already enrolled'
end
which seems pretty correct regarding the doc of Canvas about that https://canvas.instructure.com/doc/api/enrollments.html#method.enrollments_api.create
But when i run the script .... 

<Net::HTTPInternalServerError 500 Internal Server Error readbody=true>
I check the log and 

request_parameters: "{\"enrollment\"=>\"{:user_id=>\\\"575\\\", :type=>\\\"StudentEnrollment\\\", :role_id=>3, :enrollment_state=>\\\"invited\\\", :course_section_id=>99, :limit_privileges_to_course_section=>true, :notify=>true, :self_enrolled=>false}\"}"

exception_message: no implicit conversion of Symbol into Integer

I saw a couple of basic explanation http://stackoverflow.com/questions/21402111/typeerror-no-implicit-conversion-of-symbol-into-integer on google but no solution worked out.
can someone tell me what i did wrong?
Many Thanks.

Cody Cutrer

unread,
Oct 14, 2015, 11:40:01 AM10/14/15
to canvas-l...@googlegroups.com
The problem is that you are posting a nested hash as a POST param. I.e. on the server side, it got:

 { "enrollment" =>
     "<a serialized string of the entire enrollment hash>" }

Instead you need to either post a hash that looks like:

 { "enrollment[user_id]" => @check_user_exist.first.id.to_s,
   "enrollment[type]" => "StudentEnrollment",
   ... }

OR you can post a fully formed nested hash like you have as JSON as explained at http://stackoverflow.com/questions/2024805/ruby-send-json-request - just replace the `request.body = "{}"` with `request.body = data.to_json`

Cody Cutrer
Software Engineer
Instructure

--

---
You received this message because you are subscribed to the Google Groups "Canvas LMS Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to canvas-lms-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aurelien Petit

unread,
Oct 14, 2015, 11:35:25 PM10/14/15
to Canvas LMS Users
Many thanks Cody,
Really happy about this google group reactivity, two times I am asking things and always got useful answer and explanation within short time.
Indeed replacing by:
data = {
    "enrollment[user_id]" => @check_user_exist.first.id.to_s,
    "enrollment[type]" => "StudentEnrollment",
   "enrollment[role_id]" => 3,
   "enrollment[enrollment_state]" => "invited",
   "enrollment[course_section_id]" => @section.first.id.to_i,
   "enrollment[limit_privileges_to_course_section]" => true,
   "enrollment[notify]" => true,
   "enrollment[self_enrolled]" => false,
}
Worked but then I am facing another issue which is pretty weird, now the API read my data but send me that

'Couldn''t find User with API id ''576''', '51710cfa-aeef-4c5b-8b5f-660dad2a6069', 'post', '2015-10-15 03:32:48.740607', 'Ruby', 1) RETURNING "id"

But if I pass in debugging mode with rails using binding i have that 

User.find(@check_user_exist.first.id)
=> #<User id: 575, name: "xxxxx", sortable_name: "xxxx xxxx", workflow_state: "pre_registered", merge_to: nil, time_zone: nil, uuid: "chNMF7PDi25a0x2N5cjIeTFjhx91A63ePEiMtbC1", created_at: "2015-10-14 10:15:33", updated_at: "2015-10-15 03:18:01", visibility: nil, avatar_image_url: nil, avatar_image_source: nil, avatar_image_updated_at: nil, phone: nil, school_name: nil, school_position: nil, short_name: nil, deleted_at: nil, show_user_services: true, gender: nil, page_views_count: 0, unread_inbox_items_count: nil, reminder_time_for_due_dates: 172800, reminder_time_for_grading: 0, storage_quota: nil, visible_inbox_types: nil, last_user_note: nil, subscribe_to_emails: nil, features_used: nil, preferences: nil, avatar_state: nil, locale: nil, browser_locale: nil, unread_conversations_count: 0, stuck_sis_fields: "name,sortable_name", public: nil, birthdate: nil, otp_secret_key_enc: nil, otp_secret_key_salt: nil, otp_communication_channel_id: nil, initial_enrollment_type: nil, crocodoc_id: nil, last_logged_out: nil, lti_context_id: nil, crmid: 132>
Is it still about the format of the request? Because the API ask to send a string for the user_id, even thou i Tried  to replace it by an integer and still get this answer.

Rob Orton

unread,
Oct 15, 2015, 1:42:38 AM10/15/15
to Canvas LMS Users
It kind of looks like it is a string of a string. 

maybe try taking off the to_s on user_id: @check_user_exist.first.id.to_s

-Rob

Aurelien Petit

unread,
Oct 15, 2015, 10:17:59 AM10/15/15
to Canvas LMS Users
Without the to_s method it is an integer. But I also tried without and still got the same response from the server...

Cody Cutrer

unread,
Oct 16, 2015, 1:57:42 PM10/16/15
to canvas-l...@googlegroups.com
I suspect you're hitting https://github.com/instructure/canvas-lms/blob/master/app/controllers/enrollments_api_controller.rb#L418.  Most likely cause being the user doesn't have a valid pseudonym (login).

Cody Cutrer
Software Engineer
Instructure

--
Reply all
Reply to author
Forward
0 new messages