RestFul Web服务返回HTTP / 1.1 400错误请求 [英] RestFul Web service returns HTTP/1.1 400 Bad Request

查看:617
本文介绍了RestFul Web服务返回HTTP / 1.1 400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我保护了RestFul Web服务,它有几个用户可以访问的界面。用户可以在适当的授权和身份验证之后访问这些接口,即通过https。我已经为FireFox下载了一个RestFul客户端,我正在设置一些自定义标头,并且在正文中我提供了登录凭据。我能够登录到Web服务并能够访问其他接口。



但我正在使用jQuery编写代码,Ajax通过我试图称之为服务但我收到HTTP / 1.1 400 Bad Request作为回复。以下是代码





Hi I have protected RestFul web service which has several interfaces that user can access. User can access these interfaces after proper authorization and authentication i.e. over https. I have downloaded a RestFul Client for FireFox from which I am setting some custom headers and in body I am providing the login credentials. I am able to login to the Web Service and able to access the other interfaces.

But I am writing a code using jQuery, Ajax thru which I tried to call this service but I am receiving "HTTP/1.1 400 Bad Request" as a response. Following is the code


<script type="text/javascript">
    var xhr,jqXHR;
    if(!xhr && typeof XMLHttpRequest != "undefined") 
    {
        xhr = new XMLHttpRequest();
    } 

self.ajax = function(uri, method, data) {
    var request = {
        url: "https://mysite/login",
        type: "Post",
        // Content-Type: "application/json", 
        //headers: { 'My-Key': 'ACNDSDSKER$%$%$#$#$#$#$"' },
        accepts: "application/json",
        data: {  // the parameters of the request. This should be adapted to your case
            "uid": username,
            "password": password
        },
        cache: false,
        dataType: 'json',
        data: JSON.stringify(data),
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
            xhr.setRequestHeader("My-Key", btoa("ACNDSDSKER$%$%$#$#$#$#$"));
        },
        success:function (jqXHR) {
            alert("ajax success " + jqXHR.status);
        },
        error: function(jqXHR) {
            console.log("ajax error " + jqXHR.status);
        }
    };
    return $.ajax(request);
</script>





首先我尝试使用headers属性提供自定义标头,但它会出现同样的错误。其次我用beforeSend函数尝试了这个,但结果是一样的。第三,我尝试使用基本HTTP协议对登录凭据进行编码,但没有用。它给出了同样的错误。



注意: - 在Rest Client中,我不需要提供授权作为自定义标题。它使用的协议是HTTP身份验证 - OAuth协议。



请让我知道Rest Client提出的相同请求可以正常工作的原因但不是使用jQuery,Ajax编写的代码。





我也想知道我们是否需要将请求发送到证书详细信息授权用户。如果是,那么如何通过jQuery,Ajax发送它或者我们需要其他lang(java,php .....)。



请建议。



First I tried to provide custom headers using "headers" attribute but it gives the same error. Second I tried this with beforeSend function but the result is same. Third I tried to encode the login credentials using basic HTTP protocol but no use. It gives the same error.

Note:- In Rest Client I do not need to provide "Authorization" as a custom headers. Also the protocol it uses is "HTTP authentication--OAuth Protocol".

Please let me know what could be reason the same request made by Rest Client works fine but not with the code written in jQuery, Ajax.


I also would like to know whether we need to send certificate details with the request to authorise the user. If yes, then how to send it thru jQuery,Ajax or do we need to other lang like (java,php.....).

pls suggest.

推荐答案




这篇关于RestFul Web服务返回HTTP / 1.1 400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆