Delphi XE5:JSON从客户端TRESTRequest主体发送,以文本/纯文本形式接收
我正在尝试使用RESTClient(DelphiXe5,Windows8)从客户端发送JSON。但在服务器端,它作为文本/普通数据接收 我正在尝试发送的JSON:Delphi XE5:JSON从客户端TRESTRequest主体发送,以文本/纯文本形式接收,json,delphi,delphi-xe5,rest-client,Json,Delphi,Delphi Xe5,Rest Client,我正在尝试使用RESTClient(DelphiXe5,Windows8)从客户端发送JSON。但在服务器端,它作为文本/普通数据接收 我正在尝试发送的JSON: { “种类”:“新闻”, “集团”:{ “id”:“G01” }, “标题”:“最新消息”, “内容”:“这是最新消息” } 在服务器端接收到: ----------120515234155952 Content-Disposition: form-data; name="body" Content-Type: te
{
“种类”:“新闻”,
“集团”:{
“id”:“G01”
},
“标题”:“最新消息”,
“内容”:“这是最新消息”
}
在服务器端接收到:
----------120515234155952
Content-Disposition: form-data; name="body"
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
{
"kind": "News",
"group": {
"id": "G01"
},
"title": "Latest News",
"content": "this is the latest news"
}
----------120515234155952
Content-Disposition: form-data; name="access_token"
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
ya29.QQKUa6ZDsco2uDK2neuYdurolLF8LAPDjMZGTdF3bnDLOIgX1JQ8g-FxKtMLSF-gl=
MDY
----------120515234155952--
我尝试使用另一种变体,但没有任何更改:
RESTRequest.AddBody(内容)代码>
RESTRequest.AddBody(TJSONObject.ParseJSONValue(Content))代码>
RESTRequest.AddBody(TJSONObject.ParseJSONValue(Content),ctAPPLICATION\u JSON)代码>
RESTRequest.AddBody(TJSONObject.ParseJSONValue(UTF8String(Content)),ctAPPLICATION\u JSON)代码>
DoPrepareRequestBody
方法时(在unitrest.Client
上找到)TCustomRESTRequest
仅使用LParam.Name
和LParam.Value
调用MultipartPeerStream.AddFormField
。这意味着contentType始终为空,MultiPartPeerStream将其转换为text/plain
有没有办法强制其内容类型为
application/json
您可以尝试使用TJSONObject来组成主体:
var
jsResponse: TJSONValue;
jsRequest: TJSONObject;
begin
jsRequest := TJSONObject.Create();
jsRequest.AddPair('UserName', lbledtUser.Text);
jsRequest.AddPair('Password', lbledtPwd.Text);
RESTRequest.AddBody(jsRequest);
jsRequest.Free();
RESTRequest.Execute();
jsResponse := RESTRequest.JSONValue;
在服务器端,内容类型与预期相同:
var
jsReq: TJSONValue;
...
begin
...
if (CompareText(Request.ContentType, 'application/json') = 0) then
begin
jsReq := TJSONObject.ParseJSONValue(s);
end;
您可以尝试使用TJSONObject来组成主体:
var
jsResponse: TJSONValue;
jsRequest: TJSONObject;
begin
jsRequest := TJSONObject.Create();
jsRequest.AddPair('UserName', lbledtUser.Text);
jsRequest.AddPair('Password', lbledtPwd.Text);
RESTRequest.AddBody(jsRequest);
jsRequest.Free();
RESTRequest.Execute();
jsResponse := RESTRequest.JSONValue;
在服务器端,内容类型与预期相同:
var
jsReq: TJSONValue;
...
begin
...
if (CompareText(Request.ContentType, 'application/json') = 0) then
begin
jsReq := TJSONObject.ParseJSONValue(s);
end;
您可以使用此命令强制内容类型:
RESTClient1.Params.AddItem('Content-Type','application/json', TRESTRequestParameterKind.pkREQUESTBODY, [], TRESTContentType.ctAPPLICATION_JSON);
您可以使用此命令强制内容类型:
RESTClient1.Params.AddItem('Content-Type','application/json', TRESTRequestParameterKind.pkREQUESTBODY, [], TRESTContentType.ctAPPLICATION_JSON);
对我来说,解决方案是在请求中设置参数:
RESTRequest1.AddParameter('Content-Type', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
RESTRequest1.AddParameter('Accept', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
对我来说,解决方案是在请求中设置参数:
RESTRequest1.AddParameter('Content-Type', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
RESTRequest1.AddParameter('Accept', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
您的客户端发送了错误的内容类型text/plain。它应该是应用程序/json@iPathツ 你完全正确,但只是重复了问题的关键部分:o)你试过了吗?其他有用的示例:您的客户端发送了错误的内容类型text/plain。它应该是应用程序/json@iPathツ 你完全正确,但只是重复了问题的关键部分:o)你试过了吗?另一个有用的例子是:我们在服务器端使用Nodejs尝试了同样的方法,效果非常好!对我来说,服务器需要一个简单的对象,这就解决了这个问题。RestClient方法为rmPOST,其余为默认值。使用XE 10.3 Community Edition在服务器端使用Nodejs也尝试了同样的方法,这非常有效!对我来说,服务器需要一个简单的对象,这就解决了这个问题。RestClient方法为rmPOST,其余为默认值。使用XE 10.3社区版