I’m new to the Valence API and D2L(Brightspace) and I’m currently having issues with sending API Requests through my WordPress Enrollment form. Below are some steps as to what I am trying to achieve:
- A Student Signs up/enrolls in a course through an enrollment form on my WordPress site
- The enrollment passes the new student information to the Valence API doAPIRequest function in sample.js
- The Ajax call in sample.js contains an action called ‘example_wordpress_action’ which is defined in functions.php. This is where the API requests are made.
- The main problem is that when I try to use a post request, there is always a 400 Bad Request returned. However, I am able to apply a GET/ whoami request with out any issues. Is there a chance that the issues are with the way the user information is being entered?
Below is a copy of the format that is being used to send the created users information (The RoldId is being used on the D2L site and I am using method ‘POST’ and API request of ‘/d2l/api/lp/1.4/users/’):
{
"OrgDefinedId": "SBoateng",
"FirstName": "Samuel",
"MiddleName": "",
"LastName": "Boateng",
"ExternalEmail": "daerdocta@gmail.com",
"UserName": "SBoateng",
"RoleId": 103,
"IsActive": true,
"SendCreationEmail": false
}
This is what is returned from the request:
Unexpected non-JSON response from the server:
Array
(
[headers] => Array
(
[cache-control] => private
[content-length] => 0
[server] => Microsoft-IIS/7.5
[x-powered-by] => ASP.NET
[x-xss-protection] => 0
[date] => Tue, 23 Jun 2015 18:35:05 GMT
[connection] => close
)
[body] =>
[response] => Array
(
[code] => 400
[message] => Bad request
)
[cookies] => Array
(
)
[filename] =>
)
According to the documentation for the create-user route, a
400
status code as a response indicates one of two things: you’ve provided invalid data for your new user record somehow, or the back-end service was (for some reason) unable to create the new user record (very rare, but it does happen). In the second of those cases, you’d get a more detailed message in the error description indicating the failure to create. As you didn’t get this, the most likely reason is one of these:You’re not sending what you think you’re sending; it’s somehow badly formatted.
You’re providing an invalid
RoleId
— it was valid in the environment you tested under, but in the production environment, thatRoleId
doesn’t exist. This happens sometimes, but in this case, I suspect not. To verify, try fetching back theRoleId
by Id first, and view those results.You’re providing a
UserName
that already exists in the system: this property is the ID you use to identify yourself when you log in to the Brightspace system, so it has to be unique. Occasionally clients will find that the “temporary test”UserName
they’re using already exists in their back-end service, for whatever reason, and this can cause surprising failures. You can try validating by fetching back a user by thatUserName
first to validate you don’t already have a record for that user.I have since been able to solve this issue with some help. below is the code that I used, in case there are others who come across this in the future.
excerpt PHP where the form is kept:
Form submission page. The Ajax call is made in here:
$_SESSION[‘student_profile’] = $_POST;
$first_name=$_SESSION[‘student_profile’][‘name_first’];
$middle_name =$_SESSION[‘student_profile’][‘middle_name ‘];
$last_name =$_SESSION[‘student_profile’][‘last_name ‘];
$email =$_SESSION[‘student_profile’][’email ‘];
Ajax that is used in wordpress: The action used is called example_wordpress_action and it is defined in functions.php
}
Funcions.php where the request is made using wp_remote_request:
function example_wordpress_action_function() {