如何在Delphi中处理text/javascript API响应(作为application/json)?

huangapple go评论105阅读模式
英文:

How to handle text/javascript API responses in Delphi (as application/json)?

问题

当我向iTunes搜索API发出请求时,返回的内容数据以JSON格式进行格式化,但响应的内容类型是text/javascript而不是application/json

(the green one is from an API that is expected to work, but what I get from iTunes API is marked in red). 因此,Delphi的RESTDebugger接收到响应,但无法将其处理为有效的JSON('Content is not JSON')。

在IDE(Delphi 11)中,我也能够

Resp := TRequest.New.BaseURL('https://itunes.apple.com')
          .Resource('search?term=' + EditSearch.Text)
          .Accept('application/json')
          .DataSetAdapter(MemTable)
          .get;

这给我相同的200响应,但检索Resp.Content不会得到任何内容,尽管它有内容(Resp.ContentLength远大于零)。是否有办法将其转换为Delphi上可解析的JSON,以便我可以进一步处理它?
还尝试将上述命令中添加.ContentType('application/json'),但没有成功。

这是一个示例请求的URL:
https://itunes.apple.com/search?media=music&term=talk

英文:

When I make a request to the iTunes search API, the returned content data is formatted as a JSON, but the response's content-type is text/javascript instead of application/json.

:如何在Delphi中处理text/javascript API响应(作为application/json)?

(the green one is from an API that is expected to work, but what I get from iTunes API is marked in red). Thus, Delphi's RESTDebugger receives the response but cannot process it as a valid JSON ('Content is not JSON').

On the IDE (Delphi 11) I am also able to

var Resp: IResponse;
Resp := TRequest.New.BaseURL('https://itunes.apple.com')
          .Resource('search?term=' + EditSearch.Text)
          .Accept('application/json')
          .DataSetAdapter(MemTable)
          .get;

which gives me that same 200 response, but retrieving Resp.Content gets me nothing despite it has content (Resp.ContentLength is much greater than zero). Is there a way to convert it to some parseable JSON on Delphi so I can make further processing to it?
Also tried adding .ContentType('application/json') to the command above with no success.

Here is the URL for a sample request:
https://itunes.apple.com/search?media=music&term=talk

答案1

得分: 0

根据Uwe的说法,应用11.3修复程序解决了问题。

英文:

As stated by Uwe, applying the 11.3 fix solved the problem.

huangapple
  • 本文由 发表于 2023年6月26日 10:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定