15 Replies Latest reply on May 7, 2014 9:52 AM by Ryan Rutan

    Problems while using REST API to create document

    richardliu

      Hi:

       

      I followed the REST API to create docuemt within HTML widget, but it didn't work. Below are screenshots of my source code and response. What's wrong with this?

       

      Thank you

       

      Richard

       

      The function in my source code:

       

      <script type="text/javascript">

      var $j = jQuery.noConflict();

      var baseUrl = 'https://wave.aia.com';

      var jsonData = {

                     "content":{

                                "type": "text/html",

                                "text": "<body><p>Some interesting text</p></body>"

                               },

                     "subject": "New Document1",

                     "type": "document"

                     }

                  

      $j(document).ready(function() {

                   $j.ajax({

                      type:"post",

                                     url : baseUrl + '/contents',

                                     data : jsonData,

                                     dataType : "jsonp",

                                      complete : function(data) {

                                      _data = data.responseText;

                                 

                                    }

                                  })

                                })

      </script>

       

      The response of my request:

      network.pngresponse.png

       

      The REST API I followed:

      REST_API.png

        • Re: Problems while using REST API to create document
          bergerle

          You're sending your object as form-data, but the REST-API expects a JSON string in the request's data body. Instead of "data: jsonData" try "data: JSON.stringify(jsonData)"

          1 person found this helpful
            • Re: Problems while using REST API to create document
              bergerle

              please tell me if this worked. I never tried sending a request to the REST API via JavaScript, but I might need it in the future and would like to know if this is the right way.

                • Re: Problems while using REST API to create document
                  Ryan Rutan

                  Tobias Berger is correct.  I would recommend using something like PostMan for Chrome

                  https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

                   

                  And be sure to specify the content-type as application/json

                  1 person found this helpful
                    • Re: Problems while using REST API to create document
                      richardliu

                      Hi Ryan:

                       

                      You mean put the content-type in the data like this:

                      var jsonData ={

                                               "content":{

                                                                  "type": "text/html",

                                                                  "text": "<body><p>Some interesting text</p></body>"

                                                                },

                                              "subject": "New Document1",

                                               "type": "document" ,

                                               "content-type": "application/json" ,

                      }

                       

                      Or put the content-type in Ajax like this:

                       

                                  $j.ajax({

                                  async:false,

                                  type:'post',

                                  content-type: application/json,

                                  url : 'https://wave.aia.com/api/core/v3/contents',

                                        data : jsonData,

                                        dataType : 'jsonp',

                                        success : function(data) {

                                              _data = _data.replace(/^throw [^;]*;/,'');

                                              _data = data.responseText;                

                                                                                              }

                                 });

                                 

                       

                       

                      And PostMan seems to be a useful tool, I would try that.

                       

                      Thank you

                       

                      Richard

                    • Re: Problems while using REST API to create document
                      richardliu

                      Hi Tobias:

                       

                      The response body while I ran the post function would be   {   "message" : "The request could not be validated as originating from within the SBS application",   "code" : 4026 }   .

                       

                      I don't know what wrong with it.

                       

                      Do you have any idea?

                       

                      Thank  you

                       

                      Richard

                        • Re: Re: Problems while using REST API to create document
                          bergerle

                          Maybe it's a problem with authentication. You're trying to use this in a HTML widget, which runs in an iframe, so it'
                          s out of the context of the main site. Therefore your request is not authenticated and you need to add BasicAuth info to you request. You can do this by adding following to your ajax options:

                              beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password")); },

                          As you see, this requires you to supply a username and password in plain text. That user would be the creator of your content. Maybe you should rethink if a HTML widget is really the best sollution for your case.

                            • Re: Problems while using REST API to create document
                              richardliu

                              Hi Tobias:

                               

                              " beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password")); },  " , I put the actual username and password to relplace the "username" and "password", but the it return error said "btoa is undefined". And I tried encodeBase64("username:password"), but it also said 'encodeBase64' is undefined.

                               

                              Do the "username" and "password" need to be replaced by the actual username and password or not? Or is there any other wrong?

                               

                               

                              Thank you

                               

                              Richard

                            • Re: Re: Problems while using REST API to create document
                              Ryan Rutan

                              Richard Liu - Apologies, as I didn't realize you were on Jive 6.  In Jive 6, there was a security feature in place that didn't allow this use-case to happen, but in Jive 7 it can...hence the "The request could not be validated as originating from within the SBS application" message.  Also, one way to work within the

                               

                              In Jive 6, the only options that you have to have a "link" go off and do this ... is via an App Action ... but even then ... it's not the best experience.  It might be worthwhile building out middle-ware like service that takes in the request and creates the document as a service account and then presents the user with a link for them to go and edit/view.

                               

                              While Jive 7 does support this use-case better, I still would recommend against it ... as I feel the trade-offs will quickly make the final product less useful than intended.

                              1 person found this helpful