cancel
Showing results for 
Search instead for 
Did you mean: 

Post oData method in Chrome through Integration Gateway

pan_jittibophit2
Explorer
0 Kudos

Hi all,

I tried to call post method of oData service that I created. I can post it through RestClient in Chrome which is working fine. Now I tried to call post method in Chrome via Javascript to test the service because we are developing HTML5 app in Cordova.

Before I called post method, I called GET method to get X-CSRF-Token but I didn't receive any requestHeaderResponse back from the GET method. The following is my source code.


var strUsername = "XXXX";

var strPassword = "XXXX";

var sUrl = "http://XXXX:8080/gateway/odata/sap/ZGW_SPICERS_CORE_NEW_SRV/CustomerCollection";

var oHeaders = {};

oHeaders['Authorization'] = "Basic " + btoa(strUsername + ":" + strPassword);

oHeaders['x-requested-with'] = "XMLHttpRequest";

oHeaders['Content-Type'] = "application/atom+xml";

oHeaders['DataServiceVersion'] = "2.0";

oHeaders['X-CSRF-Token'] = "fetch";

var request = { headers : oHeaders,

                requestUri : sUrl,

                method : "GET" };

var res;

OData.defaultHttpClient.enableJsonpCallback=false;

OData.read(request, function(data, response) {res = response;}, function(data) {alert(JSON.stringify(data))});

I tried to capture response data in res variable. I can see return data in JSON format but there was no header data!!

I used datajs api version 1.1.1 and 1.1.2 to consume oData but it returned similar header..... I looked at request response from Chrome and Gateway definitely returned back the header data.

Is there anyone had this issue before? It would be great if anyone could help..

Regards,

Pan

Tags was edited by: jitendra kansal

Accepted Solutions (1)

Accepted Solutions (1)

bjoernw
Employee
Employee
0 Kudos

Dear Pan,

Which SMP version are you using? If I recall correctly SP3 Patch Level 01 hat some issues with the CSRF token handling. So you might want to upgrade to SP4 in case you haven't done that already.

Kind regards

Björn

pan_jittibophit2
Explorer
0 Kudos

Thanks for your input Bjoern, We'll ask our Basis guy to upgrade the SMP and see how it goes.

pan_jittibophit2
Explorer
0 Kudos

Hi Bjoern,

We've just upgraded the SMP but we still aren't able to get the headers' data returned.

Our consultant mentioned that the issue was that in the response header Access-Control-Allow-Origin is NULL

Is this some thing we can change from null to *?

Hope this makes sense.

Regards,

Pan

pan_jittibophit2
Explorer
0 Kudos

Hi Bjoern,

I've finally solved it!!!!

Basically, I inserted to many header request when read oData. Here is my javascript code for reading oData


var strUsername = "XXX";

var strPassword = "XXXX";

var sUrl = "http://XXX:8080/gateway/odata/sap/ZGW_SPICERS_CORE_NEW_SRV/CustomerCollection";

var oHeaders = {};

oHeaders['Authorization'] = "Basic " + btoa(strUsername + ":" + strPassword);

//oHeaders['x-requested-with'] = "XMLHttpRequest";

//oHeaders['Content-Type'] = "application/atom+xml";

//oHeaders['DataServiceVersion'] = "2.0";

oHeaders['X-CSRF-Token'] = "fetch";

var request = { headers : oHeaders,

                requestUri : sUrl,

                method : "GET" };

var res;

OData.defaultHttpClient.enableJsonpCallback=false;

OData.read(request, function(data, response) {res = response;}, function(data) {alert(JSON.stringify(data))});

And also I've added this code to add Access-Control-Allow-Origin: '*' in return header request in get_entityset method of oData service class


DATA: ls_header TYPE ihttpnvp.
   ls_header-name = 'Access-Control-Allow-Origin' .
   ls_header-value = '*'.
   /iwbep/if_mgw_conv_srv_runtime~set_header( ls_header ).


So now when I executed read method in javascript, I finally got all headers data back

I believe this solution is similar to this blog from Alexander

http://scn.sap.com/community/gateway/blog/2014/02/28/gateway-the-icf-and-solving-the-same-origin-pol...

Regards,

Pan

Answers (1)

Answers (1)

Jitendra_Kansal
Product and Topic Expert
Product and Topic Expert
0 Kudos

Above issue is quite similar to ours. But in our case we are using SOAP WS as one of our backend and we have modeled it as OData service using Integration gateway (SMP 3.0 SP03 PL02).

The same OData IGW URL has to be accessible directly from Java Script code and it would be a POST request. We are able to get the CSRF-TOKEN value in GET request. For POST request, Java script is not able to read the returned header from GET.

What do you suggest on this?

Regards

JK

pan_jittibophit2
Explorer
0 Kudos

Hi Jitendra,

Please try adding this code at the top of get_entityset method.

DATA: ls_header TYPE ihttpnvp. 

ls_header-name = 'Access-Control-Allow-Origin'

ls_header-value = '*'

/iwbep/if_mgw_conv_srv_runtime~set_header( ls_header ). 

We found that although we can see the return header response from Browsers (Chrome or Firefox) but we cannot access it via Javascript because Access-Control-Allow-Origin header didn't get return or null.


The code that I added in the get_entityset method is to add Access-Control-Allow-Origin with value * in the header response. This will allow me to see headers data in javascript.


Please try it and let me know how it goes.


Cheers,

Pan

Jitendra_Kansal
Product and Topic Expert
Product and Topic Expert
0 Kudos

We are converting SOAP Web services into OData services. We are not using SAP NW gateway anymore.

If we have to add above "syntex" then where we can do this?

Rgrds

JK

pan_jittibophit2
Explorer
0 Kudos

Alright I think I understand it now. I honestly tell you that I'm not sure about it since we haven't tried to get SOAP though IG yet....

As Bejoern mentioned earlier, upgrading to SP4 might help....

Sorry I cannot help much.

Regards,

Pan