I am trying to create a topic post when students complete an action. I am getting the dreaded...
I am trying to create a topic post when students complete an action. I am getting the dreaded Status 400 error : Request has missing or invalid parameters. I have checked the API reference for CreatePostData.
Here is the sample code so far :
let postString = '{"ParentPostId": null, "Subject": "This is a test post via API", "Message": { "Content": " Test Content to be posted... ", "Type": "Html" }, "IsAnonymous": false }';
let sendURL = "/d2l/api/le/1.41/"+ orgUnitId +"/discussions/forums/"+ forumId +"/topics/"+ topicId +"/posts/";
$.ajax({
url: sendURL,
type: 'POST',
data: JSON.stringify(postString ),
headers: {
'X-CSRF-TOKEN':localStorage.getItem("XSRF.Token")
},
success: function(sendresult) {
.......
I am aware of the need for the XSRF token etc, and have successfully used PUT in the past, but this is just not working. The Forum and Topics exist and are not of "group-type"... Any ideas why? Thanks....
Answers
-
@Bjorn Le Roux, thanks for the question. I have looped in our SME for some assistance. Stay tuned for an update.
-
Many thanks Judy
-
Hi @Judy Botejue - has there been any light shed on this? Thanks.
-
Again... any feedback would be appreciated... @Judy Botejue
-
Hi Richard,
I'm afraid that still returns the same result. I've tried building the data as a JS object as well and passing it as I thought it might be that. The data looks like it matches the "CreatePostData" structure, but it is still being rejected. I am attaching a screenshot with a console log of the input and returns. The two "error" returns after the XHR status are just internal debugs. Thanks.
-
Hi Bjorn,
Can you try removing the JSON.stringify( ) since you already have the body as string? I tested the body and route pattern and it works locally.
Thanks,
Richard
-
Just incase anyone else is looking at this, I think Bjorn and I figured out the issue. By default, the ajax post method is setting the Content-Type header to 'application/x-www-form-urlencoded'. The content type header must be set to 'application/json'.
-
Thank you Richard! It now works great!
For those interested, the code now looks like this :
$.ajax({
url: sendURL,
type: 'POST',
data: JSON.stringify(postString),
headers: {
'X-CSRF-TOKEN':localStorage.getItem("XSRF.Token"),
'Content-Type':'application/json'
},