cancel
Showing results for 
Search instead for 
Did you mean: 

Create Operation using ODataModel.create

Former Member
0 Kudos

Hi,

In my SAPUI5 application i am trying to create a record in backend SAP system via NW Gateway system. For this my client is using SMP3.0 SP05 as a middle-ware server through Relay server configured. First i tried using REST Client, & i am able to create it successfully after getting the X-CSRF-Token. But when i am using ODataModel.create method to create the entry, i am getting forbidden error(403). Please check my below code:-

  1. onPressSubmit:function(){ 
  2.   var newUrl = ""
  3.   newUrl = sap.app.config.smpUrl + 
  4.   sap.app.config.CreateActivity; 
  5.   jQuery.sap.require("sap.ui.model.odata.ODataModel"); 
  6.   $(function(){ 
  7.   mHeaders = { 
  8.   "X-Requested-With" : "XMLHttpRequest"
  9.                      "Content-Type" : "application/atom+xml"
  10.                      "DataServiceVersion" : "2.0"
  11.                      "X-CSRF-Token" : "Fetch"
  12.                      "X-SMP-APPCID": appCID, 
  13.                      "Access-Control-Allow-Origin": "*"
  14.   "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS"
  15.   "Access-Control-Allow-Headers": "Origin, Content-Type, Authorization, Accept, X-Request-With"
  16.   "Access-Control-Allow-Credentials": "true" 
  17.   }; 
  18.   this.createActivityModel = new sap.ui.model.odata.ODataModel(newUrl, false, "username", "password",mHeaders); 
  19.   this.createActivityModel.attachRequestFailed(function(evt) { 
  20.                 alert("Server error: " + evt.getParameter("message") + " - " + evt.getParameter("statusText")); 
  21.   }); 
  22.   this.createActivityModel.setHeaders( 
  23.               
  24.                      { 
  25.                                 "X-Requested-With": "XMLHttpRequest"
  26.     "Content-Type": "application/atom+xml"
  27.     "DataServiceVersion": "2.0",       
  28.     "X-CSRF-Token":"Fetch",  
  29.   "X-SMP-APPCID": appCID, 
  30.   "Access-Control-Allow-Origin": "http://localhost"
  31.   "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE"
  32.   "Access-Control-Allow-Headers": "Content-Type" 
  33.                      }); 
  34.   jQuery.sap.log.debug(this.createActivityModel); 
  35.             oCore = sap.ui.getCore().setModel(this.createActivityModel); 
  36.   var oEntry = {}; 
  37.   oEntry.DeviceType = sap.app.config.deviceType; 
  38.   /*this.createActivityModel.setHeaders({
  39.   "X-REQUESTED-WITH": "XMLHttpRequest",
  40.  
  41.   });*/ 
  42.       
  43.       var oEntry={ 
  44.         "IvDescription":"Test by UI Team"
  45.         "IvStartDt":"2016-03-07T00:00:00"
  46.         "IvEndDt":"2016-03-07T00:00:00"
  47.         "IvStartTime":"100000"
  48.         "IvEndTime":""
  49.         "IvStatus":"E0001"
  50.         "IvCategory":"IvActivityType"
  51.         "Text":"Test team UI"
  52.         
  53.         
  54.         
  55.   address:[ 
  56.           { "PartnerName":""
  57.           "PartnerFct":"00000009"
  58.           "PartnerId":"7000011183" 
  59.           }, 
  60.           { 
  61.           "PartnerName":""
  62.           "PartnerFct":"ZSEESP1"
  63.           "PartnerId":"EM00700112" 
  64.           }, 
  65.         ], 
  66.           
  67.          }; 
  68.        
  69.       
  70.       //this.createActivityModel.create("/AppointmentCreateStdSet",oEntry,{success: this.mySuccessHandler, error: this.myErrorHandler}); 
  71.       this.createActivityModel.create("/AppointmentCreateStdSet",oEntry,null, function doSaveLunchData_OnSuccess(oData, response) { 
  72.              alert("  doSaveLunchData_OnSuccess()"); 
  73.              logSaveData = oData.results; 
  74.            }, 
  75.            function doSaveLunchData_OnError(oError) { 
  76.              alert("  doSaveLunchData_OnError()"); 
  77.        
  78.              alert("error: "+oError); 
  79.            } 
  80.          ); 
  81.   }); 
  82.   var router = sap.ui.core.UIComponent.getRouterFor(this); 
  83.   router.navTo("newActivity", null, false);//navigation to the newActivity page using router 
  84.   }, 

Please check the response i got below:-

Please suggest.

Thanks,

Shrikant.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

Now i am getting only 1 error i.e, 403 forbidden error. I have changed the code now & using OData.request() method to create entry in backend. Please check my code below:-


onPressSubmit:function(){

  var newUrl = "";

  var token = "";

  newUrl = sap.app.config.smpUrl +

  sap.app.config.CreateActivity;

  jQuery.sap.require("sap.ui.model.odata.ODataModel");

  var f = {

  headers : {

  "X-Requested-With" : "XMLHttpRequest",

  "Content-Type" : "application/atom+xml",

  "DataServiceVersion" : "2.0",

  "X-CSRF-Token" : "Fetch",

  "X-SMP-APPCID": appCID,

  },

  requestUri : newUrl,

  method : "GET"

  };

  var parameters={

        "IvDescription":"Test by UI Team",

        "IvStartDt":"2016-03-07T00:00:00",

        "IvEndDt":"2016-03-07T00:00:00",

        "IvStartTime":"100000",

        "IvEndTime":"",

        "IvStatus":"E0001",

        "IvCategory":"IvActivityType",

        "Text":"Test team UI",

      

      

      

  "address":[

          { "PartnerName":"",

          "PartnerFct":"00000009",

          "PartnerId":"7000011183"

          },

          {

          "PartnerName":"",

          "PartnerFct":"ZSEESP1",

          "PartnerId":"EM00700112"

          },

        ],

        

         };

  OData.request(f, function(data, oSuccess) {

  oToken = oSuccess.headers['x-csrf-token'];

  token = oSuccess.headers['x-csrf-token'];

   var cookie = oSuccess.headers['Set-Cookie'];

  /* oHeaders = {

  "x-csrf-token" : oToken,

  };*/

   OData.request({

                   requestUri : newUrl+"/AppointmentCreateStdSet",

                headers : {

                 'X-Requested-With': 'X', 

   "Content-Type": "application/json",

   'Accept' : 'application/json', 

  "DataServiceVersion" : "2.0",

  "X-CSRF-Token" : token,

  "X-SMP-APPCID": appCID,

  "Access-Control-Allow-Origin": "*",

             "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS",

             "Access-Control-Allow-Headers": "Origin, Content-Type, Authorization, Accept, X-Request-With",

             "Access-Control-Allow-Credentials": "true",

             'Authorization' : 'Basic ' + btoa("username" + ':password'),

             "Cookie":cookie

            

            

  },

                   method : "POST",

                

                   data:parameters

       },

                   function(data,request) {

                   alert("Employee Created Successfully");       

                   location.reload(true);

       },          function(err) {

                   alert("Employee Creation Failed");

       });

  

  });

In chrome i am getting following response 403 forbidden error as shown below.

I don't understand why i am getting "x-csrf-token:Required" as i can see token is successfully posted in the Request Header.

I have created another thread for this new code here, please check: https://scn.sap.com/message/16670990#16670990

Thanks,

Shrikant.

Former Member
0 Kudos

How can i check that problem might be from SMP server or backend system, however from REST Client it is working perfectly & gets 201 created in response.

I have already checked in backend that "~CHECK_CSRF_TOKEN=0" in SICF.

Thanks,

Shrikant.

sreehari_vpillai
Active Contributor
0 Kudos

any errors in /IWFND/ERROR_LOG when you try from SMP ?

Former Member
0 Kudos

Hi Srikant,

No need of passing "X-CSRF-TOKEN" while using oData model, the model adds the token to its header while it does a metadata load. Please remove the header and try again.

Let me know if it works !

Thanks,

Vinuta

Former Member
0 Kudos

Hi ,

I have tried without X-CSRF-TOKEN, but still it doesn't work & getting 403 Forbidden Error.

Please Suggest.

Thanks,

Shrikant.

PMarti
Active Participant
0 Kudos

Hi Naidu, try to refresh token with method of odata v2 model refreshSecurityToken  (Link) and do the create operation in success callback of that method.

Regards,

Pau

sreehari_vpillai
Active Contributor
0 Kudos

post a screenshot from the response tab.

why are you passing " "X-CSRF-Token":"Fetch",    in the request headers ? how ever you are not making any manual GET operation to fetch the CSRF manually.


Are you able to POST successfully from gateway client with the same payload ?

Sree

Former Member
0 Kudos

Hi ,

Thanks for reply.

Do you mean i don't need to pass the "X-CSRF-Token":"Fetch" ?

Does ODataModel.create passes the Token automatically? If yes then how the token will be passed while POST service call.

Here are attached screenshot.

Please suggest.

Thanks,

Shrikant