rest - 如何在 Delphi 中的 REST 响应中转发带有特殊字符的文本?

标签 rest delphi special-characters delphi-xe7

请帮忙。我正在尝试学习 REST 概念并使用 Delphi 和 REST 对象制作第一个程序。我遇到了一个我不知道如何解决的问题。在数据库中,我对我的母语(波斯尼亚语)中的特殊字符进行了测试:č、ć、đ、š、ž。当我通过 GET 方法传递此文本时,在表单上的对象中解析和显示此文本的对象会将这些字符显示为“?”、“æ”、“æè”等。 我研究并尝试解决该问题,但没有成功。我尝试使用 Utf8ToAnsi 函数,并在 RESTClient、RESTRequest 和 RESTResponce 对象参数中放入 iso-8859-2 而不是 UTF-8。 请就如何解决或调查此问题提供帮助、指导或建议。

服务器端源代码:

procedure TWebModule1.UsersGet(Request: TWebRequest; Response: TWebResponse);
var
  a: TJSONArray;
  o: TJSONObject;
  i: Integer;
  Q1: TADOQuery;
begin
  Q1:=TADOQuery.Create(nil);
  Q1.Connection:= BasicDBConn.Konekcija;
  with Q1 do
  begin
    Active:=False;
    SQL.Clear;
    SQL.Add('Select USER_ID, USER_NAME, ROLE_NAME  from USERS, ROLES  ' +
      ' where USERS.ROLE_ID=ROLES.ROLE_ID ');
    Active:= True;
  end;
  a := TJSONArray.Create;

  if Q1.RecordCount > 0 then
  begin
    for i:=1 to Q1.RecordCount do
    begin
      o := TJSONObject.Create;
      o.AddPair('USER_ID', Q1.Fields.Fields[0].Value);
      o.AddPair('USER_NAME', Q1.Fields.Fields[1].Value);
      o.AddPair('ROLE_NAME', Q1.Fields.Fields[2].Value);
      Q1.Next;
      a.AddElement(o);
    end;
  end;

  Response.ContentType := 'application/json';
  Response.Content := a.ToString;
  a.DisposeOf;
  Q1.Free;
end;

在客户端:

procedure TGlavna.Button1Click(Sender: TObject);
begin
  RESTRequest.Resource := 'Users';
  RESTRequest.Method   := TRESTRequestMethod.rmGet;
  RESTRequest.Response := RESTResponse;

  RESTRequest.Execute;

  if assigned(fJSONArray) then fJSONArray.DisposeOf;
  fJSONArray := TJSONObject.ParseJSONValue(RESTResponse.Content) as TJSONArray;

  if RESTResponse.Content.IsEmpty then Application.MessageBox('Empty', 'Information', MB_OK)
    else Memo1.Lines.Add(RESTResponse.Content);
end;

这就是数据库中的数据的样子

USER_ID USER_NAME ROLE_ID u1 测试用户 2
u2 T_č_ć_š_đ_ž 1
u3 t_Č_Ć_Ž_Đ_Š 1
u4 布拉迪克·凯南 1

在网络浏览器中,响应数据是:

[{"USER_ID":"u1","USER_NAME":"测试用户","ROLE_ID":"2"},{"USER_ID":"u3","USER_NAME":"t_È_Æ_Ž_Ð_Š","ROLE_ID ":"1"},{"USER_ID":"u4","USER_NAME":"Bradiæ Kenan","ROLE_ID":"1"},{"USER_ID":"u2","USER_NAME":"T_è_æ_š_ð_ž","ROLE_ID":"1"}]

Headers in web browser:
General:
    Request URL: http://localhost:8080/Users
    Request Method: GET
    Status Code: 200 OK
    Remote Address: [::1]:8080
    Referrer Policy: strict-origin-when-cross-origin
Response Headers:
    Connection: close
    Content-Length: 228
    Content-Type: application/json; charset=ISO-8859-1
    Date: Mon, 27 Dec 2021 17:07:54 GMT
Request Headers:
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-US,en;q=0.9
    Cache-Control: max-age=0
    Connection: keep-alive
    Host: localhost:8080
    sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"
    sec-ch-ua-mobile: ?0
    sec-ch-ua-platform: "Windows"
    Sec-Fetch-Dest: document
    Sec-Fetch-Mode: navigate
    Sec-Fetch-Site: none
    Sec-Fetch-User: ?1
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

谢谢...

最佳答案

HTTP 响应包含此行:

Content-Type: application/json; charset=ISO-8859-1

因此客户端将响应内容解释为 ISO-85591 编码而不是 UTF-8。 您的服务器端代码包含此行:

Response.ContentType := 'application/json';

您的服务器端代码应清除 charset=ISO-85591 部分。

关于rest - 如何在 Delphi 中的 REST 响应中转发带有特殊字符的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70485961/

相关文章:

c# - VB6 到 C# XML 字符串转换特殊字符

javascript - React fetch, “credentials: include” 中断了我的整个请求,我得到了一个错误

Delphi VCL 样式教程 - 如何在运行时更改样式

c++ - 将 Delphi 类传递给需要具有 __thiscall 方法的类的 C++ 函数/方法

java - 使用特殊编码将 Java String 写入文件

php - 如何用 PHP 中的特殊字符替换特殊字符?

c++ - 如何将字母字符转换为相应的转义特殊字符

rest - PayPal:无法创建沙箱帐户和 REST API 帐户

rest - 对于损坏的有效负载(校验和失败),最合适的 HTTP 错误代码是什么?

spring - 使用不同的@QueryParams 重载路径函数