I am trying to create a topic post when students complete an action. I am getting the dreaded...

Bjorn.L.964
Bjorn.L.964 Posts: 6 🌱
edited November 2022 in Development

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....

Tagged:

Answers

  • Judy.B.556
    Judy.B.556 Posts: 48 admin
    edited November 2022

    @Bjorn Le Roux​, thanks for the question. I have looped in our SME for some assistance. Stay tuned for an update. 

  • Bjorn.L.964
    Bjorn.L.964 Posts: 6 🌱
    edited November 2022

    Many thanks Judy

  • Bjorn.L.964
    Bjorn.L.964 Posts: 6 🌱
    edited November 2022

    Hi @Judy Botejue​  - has there been any light shed on this? Thanks.

  • Bjorn.L.964
    Bjorn.L.964 Posts: 6 🌱
    edited November 2022

    Again... any feedback would be appreciated... @Judy Botejue​ 

  • Bjorn.L.964
    Bjorn.L.964 Posts: 6 🌱
    edited November 2022

    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.

    API discussion post problem 1

  • Richard.M.314
    Richard.M.314 Posts: 16
    edited November 2022

    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

  • Richard.M.314
    Richard.M.314 Posts: 16
    edited November 2022

    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'.

  • Bjorn.L.964
    Bjorn.L.964 Posts: 6 🌱
    edited November 2022

    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'

              },