[英]getting BAD REQUEST error for simple GET method REST API using Poco::Net::HTTPClientSession in C++
RESTful 服務可用並且它使用 curl GET 方法給出正確的響應。 它具有基本的身份驗證機制。 下面是命令和輸出。
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": "UIUGY": ", "PER_ID": "10001", "PER_PASSPORTNUM": "4564TRT2342", "TBROWID": 9001}
現在對於相同的 GET 請求和身份驗證,我使用的是 C++ Poco 庫,它收到 BAD REQUEST 錯誤。 不知道為什么,我盡我所能嘗試了更改,但仍然是同樣的錯誤。 我認為授權在這里正確發生。 下面是相同的示例代碼。 任何想法如何解決這個問題。
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;
}
如果我運行二進制文件,這是輸出:
為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)
好吧,我的錯.. 閱讀http://www.faqs.org/rfcs/rfc2616.html 后,意識到由於標頭信息或 uri 信息設置不正確導致出現錯誤請求。
必須在 setMethod(HTTP_GET) 之后將 URI 設置為if 部分代碼:
request.setURI(uri.getPathAndQuery());
它工作正常。 我得到正確的回應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.