Skip to Content
author's profile photo Former Member
Former Member

How to set authorized for REST service using SAPUI5

Dear all,

I am using the Rest service for sending and getting the data in json format.But i am calling the rest service i need to authorization is required.I am passing the userid and password.But i am getting this error 401 Unauthorized.


$.ajax({ url : "urlrestservice", type : "POST", data : jsonData, dataType : "json", username : "xxxxxx", password : "xxxxxxxx", cache : false, crossDomain : true, contentType : "application/json", success : function(data, textStatus, jqXHR) { console.log("SUCCESS"); }, error : function(xhr, readyState) { console.log("ERRO :"+readyState); } });

Please tell me the how to pass the userid and password.

Thanks and Regards,

Durga Rao.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Sep 29, 2015 at 08:33 AM

    Durga, your syntax is correct. But I think you are facing the origin policy.

    This means, that your rest service needs to be on the same server as your webservice where your sapui5 application is hosted.

    If they are on different server a security policy forbids your rest call. Therefore you are getting the 401.

    1. Workaround (short term solution):

    Use jsonp (json with padding) instead of json

    http://stackoverflow.com/questions/2699277/post-data-to-jsonp

    2: Solution:

    Solving "Same Origin Policy" Issue in different ways

    Use a SimpleProxyServlet for Testing to Avoid Cross-domain Requests - UI Development Toolkit for HTML5 (SAPUI5) - SAP Li…

    Cheers,

    Christian

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Durga Rao,

      Could you please elaborate what your are trying to achieve.

      1. In which server your UI5 application is running. If both are in different servers how you going to handle the same origin policy, Since you are not having sap web dispatcher ? (jsonp is not advisable for productive use as said by Neu)

      2. Is the rest service is newly implemented for UI5 application or existing one. Did the rest service test is fine with rest-clients via browsers.

      3. Is the rest service will support post.( I mean are you reading some data and doing some update or create operation).

      Share more details on how you testing the rest service .

      Thanks

      Rabin D

  • author's profile photo Former Member
    Former Member
    Posted on Oct 13, 2015 at 04:55 AM

    Hi All,

    Final i got the RestWebServiceClient in java.the below code for using and getting the data.

    Client client = Client.create();

    WebResource webResource = client.resource("http://localhost:8080/xxxx/xxx/webservice/xxx/"+xdsdcc);

    ClientResponse response = webResource.accept("application/xml").get(ClientResponse.class);

    if (response.getStatus() != 200) {

    throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());

    }

    String output = response.getEntity(String.class);

    Thanks

    Durga Rao.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 29, 2015 at 08:33 AM

    Hi,

    try changing ur parameter type:"POST" to "GET".If you need to do a post operation first you need to obtain x-csrf token using get service.For just reading a service GET call is enough

    Regards

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 07, 2015 at 09:15 AM

    Hi Christian and Rabin,

    sorry for the late reply.

    @Christian,

    The rest service is working in rest client into google chrome.SAP EP system and back end systems are oracle and PI.PI system we will get WSDL and Rest services.

    @Rabin,

    SAPUI5 application running in SAP EP system only.But back end we are using different systems.


    My rest service are only support POST request only.



    Final we are try to use the wsdls and rest services in java component.



    Please tell me the java code for this requirement.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.