cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle POST request in an xsjs file?

Former Member
0 Kudos

There is many examples to handle a data, which was send by GET request to on server side JavaScript.

However, until now I could not found one similar example for the POST request processing.

Can anyone provide me a link for such an example?

Kind regards

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

What specifically about the POST are you having problems with.  The main difference I see between GET and POST is the fact that instead of URL parameters, the input is generally in the Request Body. Here is the API call to read the Request Body:

     // read request body

     var content = $.request.entities[0].body.asString();

Is that what you are looking for?

Former Member
0 Kudos

Hi Thomas,

yes. I don't see my payload at the server side.

after the request:

// request

var formData = JSON.stringify(sap.ui.getCore().byId("projectConfForm").getCustomData()[0].getValue("formValues"));
$.ajax({

                url: '../services/projectMaintainanceFunctions.xsjs?ACTION=ADD_PROJECT',

                type: 'POST',

                contentType: 'application/json',

                data: formData,

                dataType: 'json',

                success: function(ajaxResult){},

                error:  function(jqXHR, textStatus, errorThrown){

                    sap.ui.commons.MessageBox.show(jqXHR.responseText, "ERROR", "Service call error");

                }

            });

both $.request.body and $request.entities at the server side are 'undefined'.

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Your right - I took that example from a multi-part form POST. Therefore there were multiple entities in that example.  In a simpler POST like you have I simply use:
     var content = $.request.body.asString();

I created a little test UI:

<!DOCTYPE HTML>

<html>

       <head>

              <meta http-equiv="X-UA-Compatible" content="IE=edge">

   <title>Test</title>

 

     <script src="/sap/ui5/1/resources/sap-ui-core.js"

                      id="sap-ui-bootstrap"

                      data-sap-ui-libs="sap.ui.ux3,sap.ui.commons"

                      data-sap-ui-theme="sap_bluecrystal" >

              </script>

              <!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->

   <script type="text/javascript">

     function callService(){

  var oBusinessPartner = {};

  oBusinessPartner.PARTNERID = "0000000000";

  oBusinessPartner.EMAILADDRESS = "test@sap.com";

  $.ajax({

                 url: '/playground/test/post.xsjs?ACTION=ADD_PROJECT',

                 type: 'POST',

                 contentType: 'application/json',

                 data: JSON.stringify(oBusinessPartner),

                 dataType: 'json',

                 success: function(){alert("Success")},

                 error:  function(jqXHR, textStatus, errorThrown){

                     sap.ui.commons.MessageBox.show(jqXHR.responseText, "ERROR", "Service call error");

                 }

             });

     }

    

  var myButton = new sap.ui.commons.Button("btn");

  myButton.setText("Hello World Demo");

  myButton.attachPress(function(){callService()});

  myButton.placeAt("content");

  </script>

     <script>

     </script>

       </head>

       <body class="sapUiBody" role="application">

              <div id="content"></div>

       </body>

</html>

I checked in the debugger and it posts just fine and I can see the content:

TomK
Advisor
Advisor
0 Kudos

Hi Thomas,

I using the sap.ui.commons.FileUploader to send data to a XSJS, which should save the picture into a hana NCLOB column.

So far I can select for example a CSV-file with a string contained in it. It is getting posted to the db-table correctly. Until now did not manage to upload PNG-File.

Excerpt: Sending file via Fileloader.

------------------------------------------------------------

        var dataUrl = location.origin + "/SAPUI5onHANAapp/services/savePicture.xsjs";

        var oFileloader = Fileloader;

        var fileName = oFileloader.getValue();

        url = dataUrl + "?file_name=" + fileName;

        oFileloader.setUploadUrl(url);

        oFileloader.upload();

__________________________________

Excerpt: XSJS - get body as String

__________________________________

var file_body = $.request.entities[0].body.asString(); // -> will be inserted into db-table

__________________________________

Do you have any advice, what I need to enhance to save an image successfully to hana? I guess I need to enhance the $request handling. Here my code crashes, when trying to upload a PNG-File.

Thanks and best regards

Tom

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

You certainly get do body.asString() for an binary file type. use body.asArrayBuffer() instead.  This will keep the content in its original binary form and keep from corrupting it.

former_member184970
Active Participant
0 Kudos

Hi Thomas,

In the the example that you have given, you have created an object

oBusinessPartner.PARTNERID = "0000000000";

  oBusinessPartner.EMAILADDRESS = "test@sap.com";


and sent to the server thru HTTP Post.


at the server side, to get the request body


var content = $.request.entities[0].body.asString();


How do I get the values for oBusinessPartner.PARTNERID,oBusinessPartner.EMAILADDRESS  from the variable content ?


Regards

Veera

lucas_oliveira
Advisor
Advisor
0 Kudos

Hi Veera,

Heads up for .

Please don't highjack/resurrect old-answered threads. Create new ones if necessary.

Thanks,

Lucas de Oliveira (moderator)

0 Kudos

Hi Thomas, I am getting a "Cannot defined asString for undefined" as an error, and I cannot seem to post the data from my other application to this current one because of that. Can you please let me know as to how do I handle that error.

Best Regards,

Kanishka

Answers (0)