Celebrate Excellence in Education: Nominate Outstanding Educators by April 15!
Found this content helpful? Log in or sign up to leave a like!
Hey. I'm trying to loop through a csv file to enroll students into a course but I keep getting a 404 error. Can anyone give me some advice as to why? When I switch the request method to GET from POST I get a 200 status code. I've been staring at this for a while now so maybe I'm over looking something?
record | course_id | user_id | role | status |
Solved! Go to Solution.
Thanks everyone for their help. I was able to get it solved, and am posting my updated code for anyone that may have the same issue in the future. Turns out it was the "enrollment[course_section_id] " parameter that was throwing the error. According to the API documentation, this parameter is skipped over if the section ID is in the URL being passed. Since it is in my instance, then I could remove it. By removing it, I was successfully able to make the API call to bulk enroll users with specific roles in the course!
import requestsimport jsonimport csvcsv_filename = '[PATH TO MY CSV FILE]'with open(csv_filename) as f:reader = csv.reader(f)lst = list(reader)secret_token = [MY API KEY]headers = {'Authorization' : 'Bearer ' + secret_token}courseID = [MY COURSE ID NUMER]url = "https://[MY INSTITUTION].instructure.com/api/v1/courses/" + str(courseID) + "/enrollments"for x in ndarray:userID = x[2]enrollmentType = x[3]print(userID)payload = {'enrollment[user_id]': userID, 'enrollment[type]': enrollmentType}r = requests.post(url,headers = headers, data = payload)print(r.status_code)
Hi @bertscjn,
I'm not familiar with numpy or pandas, so I can't quite decipher your whole code, but here are a couple things I note (some may be from copy/paste though):
Maybe one of the above will help. If not, post back and I'm sure others will chime in with more suggestions.
-Chris
In your payload, you are using the variable a for the enrollment user ID, but I don't see the variable a defined.
As @chriscas mentioned, it also looks like your indentation is off at the end of your code since the payload, request, and print aren't indented under the for loop.
If you are still experiencing issues, it would also help to know more about the CSV file you are using (at least what the columns are).
@JamesSekcienski , Thanks for the response and noticing I didn't declare my "a" variable. I meant to update that to "course" to reflect the course variable but didn't before posting. Sorry about that. However, doing so didn't help; neither indenting the "payload = ..." and "r = ...". I'm still getting the 404 error. My CSV file's column headers are:
record | course_id | user_id | role | status |
@chriscas , Thanks for your response, too. I changed "params" to "data" but still got a 404 error. Also, printing the URL does look valid.
Thanks everyone for their help. I was able to get it solved, and am posting my updated code for anyone that may have the same issue in the future. Turns out it was the "enrollment[course_section_id] " parameter that was throwing the error. According to the API documentation, this parameter is skipped over if the section ID is in the URL being passed. Since it is in my instance, then I could remove it. By removing it, I was successfully able to make the API call to bulk enroll users with specific roles in the course!
import requestsimport jsonimport csvcsv_filename = '[PATH TO MY CSV FILE]'with open(csv_filename) as f:reader = csv.reader(f)lst = list(reader)secret_token = [MY API KEY]headers = {'Authorization' : 'Bearer ' + secret_token}courseID = [MY COURSE ID NUMER]url = "https://[MY INSTITUTION].instructure.com/api/v1/courses/" + str(courseID) + "/enrollments"for x in ndarray:userID = x[2]enrollmentType = x[3]print(userID)payload = {'enrollment[user_id]': userID, 'enrollment[type]': enrollmentType}r = requests.post(url,headers = headers, data = payload)print(r.status_code)
To participate in the Instructure Community, you need to sign up or log in:
Sign In