为什么 JsonCpp http 客户端在 201 响应代码上失败?

Why does JsonCpp http client fail on a 201 response code?

使用 json-rpc-cpp library, I am creating an EOS Wallet using wallet RPC.

HttpClient *temp = new HttpClient("http://127.0.0.1:30031/v1/wallet/create"); 

string res;
string str = "testwallet1";
temp->SendRPCMessage(str, res);
cout<<"res : "<<res<<endl;

创建钱包成功,但之后出现以下异常。

unknown file: Failure
C++ exception with description "Exception -32603 : INTERNAL_ERROR: : "PW5JcEu7jTXd7XUYLWkPuCUbr1pqBhusqRFfhSVToqUNcDuZ3oeYK"" thrown in the test body.

我发现 HttpClient 收到 201 响应代码。我不知道如何避免该异常。有人知道吗?

201 基本上意味着您的请求已成功处理。正如 this source 解释的那样:

201 CREATED The request has been fulfilled and has resulted in one or more new resources being created.

The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI.

The 201 response payload typically describes and links to the resource(s) created. See Section 7.2 of RFC7231 for a discussion of the meaning and purpose of validator header fields, such as ETag and Last-Modified, in a 201 response.

对响应数据进行任何进一步处理时必须抛出异常。
没有更多信息,我无法确定到底是什么原因造成的。

此问题是由 HttpClient::SendRPCMessage() 实施中的错误引起的。

在内部,HttpClient 使用 libcurl 进行 HTTP 处理,在 SendRPCMessage() 实现的最后是以下检查 curl_easy_perform() 是否成功:

long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);

if (http_code != 200) {
  throw JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR, result);
}

如您所见,SendRPCMessage() 抛出异常 任何 HTTP 响应代码不是 200。但是 per the HTTP standard, ALL 2xx response codes indicate success, not just 200. In this case, response code 201 意味着:

10.2.2 201 Created

The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field. The response SHOULD include an entity containing a list of resource characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. The origin server MUST create the resource before returning the 201 status code. If the action cannot be carried out immediately, the server SHOULD respond with 202 (Accepted) response instead.

A 201 response MAY contain an ETag response header field indicating the current value of the entity tag for the requested variant just created, see section 14.19.

这明显是SendRPCMessage()实现中的逻辑错误。 http_code 的检查应该更像这样:

if ((http_code / 100) != 2)

这会将所有 2xx 响应代码视为成功。

我已经向 json-rpc-cpp 的作者提交了错误报告:

#278 HttpClient::SendRPCMessage() throws ERROR_RPC_INTERNAL for successful HTTP responses