1

In my java webapplication i have added a webservice call @ URL 10.10.10.21:8088/openbravo/com.data.service.

When i hit the URL from browser, it asks me for a Username and password. After entering those details, i can see the json output (desired output).

The web service provides a JSON REST service. I want to implement a basic client UI to fetch this JSON data and publish on the page. As a part of client i have written basic html code with jQuery ajax to make call to this URL. But i get 401 unauthorized response.

$("input#add-contact").on("click", function () {
            $.ajax({
            type: "POST",
            url: "http://10.10.10.21:8088/openbravo/com.indavest.openbravo.restaurantpos.service",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {
                console.log(response);
            }
            });
        });

How can i add the authentication headers to the call. Is the above lines of code the right approach to make call to the rest webservice. I am new to the Webservices concepts. Any help would be great!!! Thanks.

1 Answer 1

1

for basic authentication:

$.ajax({
type: "post",
url: "http://localhost:8080/myservice/function?format=json",
headers: { "Authorization" : makeBasicAuth("1", "1") },
data: jsonText,
contentType: "application/json; charset=UTF-8",
dataType: "json",
success: function( data, statusTxt, xjobj )
    {
        showSuccess( data, statusTxt, xjobj, "blabla" );
    },
            error: function( xjobj, statusTxt, errorThrown )
            {
        showError( statusTxt, xjobj, errorThrown, "blabla" );
        }
    });

with:

function makeBasicAuth(user, password)
{
    var tok = user + ':' + password;
    var hash = btoa(tok);
    return "Basic " + hash;
}

but this is for basic, there is also digest authentication (I recommend you wikipedia for a great explanation of those http technics) and so son, but the idea is similar.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! for the response.. in the code - data: jsonText .. what is jsonText here?
@SangramAnand This is the payload, the HTTP body.
I tried with the above piece of code, its reaching the server, but the Authorization header is going null. Am i missing any params here ?
the authorization header is null? this is strange, have you checked it with fiddler?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.