简体   繁体   English

在 C++ 中使用 Poco::Net::HTTPClientSession 获取简单 GET 方法 REST API 的 BAD REQUEST 错误

[英]getting BAD REQUEST error for simple GET method REST API using Poco::Net::HTTPClientSession in C++

RESTful Service is available and it is giving correct response using curl GET Method. RESTful 服务可用并且它使用 curl GET 方法给出正确的响应。 It has basic Authentication mechanism.它具有基本的身份验证机制。 Below is command and output.下面是命令和输出。

curl --header "Authorization: Basic YWw6NG01RzZsOG41UDlpMXAzTjZzOGQ=" 'http://localhost:8153/api.rsc/FBI_dbo_PERSON(per_id=10001)' curl --header "授权:基本 YWw6NG01RzZsOG41UDlpMXAzTjZzOGQ="'http://localhost:8153/api.rsc/FBI_dbo_PERSON(per_id=10001)'

{"@odata.context":"http://localhost:8153/api.rsc/$metadata#FBI_dbo_PERSON/$entity", "ALERTLEVEL": "3 ", "DATEENTERED": "1966-09-12T00:00:00.0000+05:30", "PER_CITIZENOF": "SDASDAS", "PER_DOB": "1944-06-11T00:00:00.0000+05:30", "PER_FamilyName": "GHGH", "PER_FirstName": "UIUY", "PER_ID": "10001", "PER_PASSPORTNUM": "4564TRT2342", "TBROWID": 9001} {"@odata.context":"http://localhost:8153/api.rsc/$metadata#FBI_dbo_PERSON/$entity", "ALERTLEVEL": "3 ", "DATEENTERED": "1966-09-12T00:00 :00.0000+05:30", "PER_CITIZENOF": "SDASDAS", "PER_DOB": "1944-06-11T00:00:00.0000+05:30", "PER_FamilyName": "GHGH", "PER_FirstName": "UIUGY": ", "PER_ID": "10001", "PER_PASSPORTNUM": "4564TRT2342", "TBROWID": 9001}

Now for same GET request and authentication, I am using C++ Poco library, it am getting BAD REQUEST error.现在对于相同的 GET 请求和身份验证,我使用的是 C++ Poco 库,它收到 BAD REQUEST 错误。 Not sure why, I tried changes at my end where I could, but still same error.不知道为什么,我尽我所能尝试了更改,但仍然是同样的错误。 I think Authorization is happening correctly here.我认为授权在这里正确发生。 Below is the sample code for the same.下面是相同的示例代码。 Any idea how to resolve this.任何想法如何解决这个问题。

int main(){

std::string sRet;
std::string sURL= "http://localhost:8153/api.rsc/FBI_dbo_PERSON(per_id=10001)";
std::string m_sAuthorization = "Basic YWw6NG01RzZsOG41UDlpMXAzTjZzOGQ=";
std::string sRESTMethod = "GET";

try
{
        Poco::URI uri(sURL);
        Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());      
        Poco::Net::HTTPRequest request;         
        request.setURI(sURL);
        session.setKeepAlive(true);
        request.setKeepAlive(true);         
        request.add("Authorization", m_sAuthorization);
        printf("Making HTTPRequest for <%s> \n", sURL.c_str());
        if(sRESTMethod.compare("GET")==0)
        {
            try
            {
                request.setMethod(Poco::Net::HTTPRequest::HTTP_GET);                                
                session.sendRequest(request);
            }
            catch(Poco::Exception &ex)
            {
                printf("Exception trying to send request<%s>. \n", sURL.c_str());
                printf("Exception message<%s> \n", ex.message().c_str());
            }
        }

        Poco::Net::HTTPResponse response;
        std::istream& rs = session.receiveResponse(response);
        std::string sResponseReason = std::string(response.getReason());
        std::ostringstream oss;
        Poco::StreamCopier::copyStream(rs, oss);            
        sRet = oss.str();
        std::cout << "Response Status : " << response.getStatus() << std::endl;
        if(sResponseReason.compare("OK")==0)
        {
            printf("HTTPReponse OK :::  %s for <%s> \n", sResponseReason.c_str(), sURL.c_str());
            if(!sRequestContent.empty())
                printf("Request Content is <%s> \n", sRequestContent.c_str());
        }
        else
        {
            printf("HTTPReponse NOT OK ::  %s:<%s> for <%s> \n", sResponseReason.c_str(), sRet.c_str(), sURL.c_str());
            sRet = "";
        }
}
catch (Poco::Exception &ex)
{        
    printf("Exception<%s> \n", ex.displayText().c_str());
    sRet = "";
}
std::cout <<  sRet << std::endl;
return 0;

} }

This is the output if I run the binary :如果我运行二进制文件,这是输出:

Making HTTPRequest for http://localhost:8153/api.rsc/FBI_dbo_PERSON(per_id=10001) Response Status : 400 HTTPReponse NOT OK :: Bad Request:<Bad Request> for http://localhost:8153/api.rsc/FBI_dbo_PERSON(per_id=10001)http://localhost:8153/api.rsc/FBI_dbo_PERSON(per_id=10001)发出 HTTPRequest 响应状态:400 HTTPReponse NOT OK :: Bad Request:<Bad Request> for http://localhost:8153/api.rsc/ FBI_dbo_PERSON(per_id=10001)

Ok, my bad.. After reading the http://www.faqs.org/rfcs/rfc2616.html realized the Bad Request was coming due to the header information or uri information not set correctly.好吧,我的错.. 阅读http://www.faqs.org/rfcs/rfc2616.html 后,意识到由于标头信息或 uri 信息设置不正确导致出现错误请求。

Had to set URI after setMethod(HTTP_GET) in if part of code as :必须在 setMethod(HTTP_GET) 之后将 URI 设置为if 部分代码

request.setURI(uri.getPathAndQuery()); request.setURI(uri.getPathAndQuery());

And it worked correctly.它工作正常。 I am getting correct response.我得到正确的回应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM