如何强制TRESTRequest.ContentType为ctAPPLICATION_JSON
将如何强制TRESTRequest.ContentType为ctAPPLICATION_JSON,rest,delphi,Rest,Delphi,将TRESTClient、TRESTRequest和TRESTRepsonse放到表单上 设置RESTClient.BaseURL、RESTRequest.Method和Resource,还添加一个值为“application/json”的“Content Type”头参数 使用RESTRequest.AddBody添加JSON字符串,然后查看RESTRequest.ContentType 它显示的是ctAPPLICATION\u X\u WWW\u FORM\u URLENCODED,而不是c
TRESTClient
、TRESTRequest
和TRESTRepsonse
放到表单上
设置RESTClient.BaseURL
、RESTRequest.Method
和Resource
,还添加一个值为“application/json”
的“Content Type”
头参数
使用RESTRequest.AddBody
添加JSON字符串,然后查看RESTRequest.ContentType
它显示的是ctAPPLICATION\u X\u WWW\u FORM\u URLENCODED
,而不是ctAPPLICATION\u JSON
。这会导致服务器在运行RESTRequest.Execute()
时返回错误
当属性无法分配给时,如何强制请求使用正确的内容类型?查看
REST.Client
源代码后,如果您使用以下方法指定内容类型:
AParameter := RESTRequest.Params.AddItem;
AParameter.ContentType := ctAPPLICATION_JSON;
AParameter.name := 'Content-Type';
AParameter.Value := 'application/json';
与此相反:
RESTRequest.Params.AddHeader('Content-Type', 'application/json');
然后TRESTRequest.ContentType
属性返回正确的值,这是TRESTRequest.Execute
期间使用的值
另一种在使用正文时强制执行TRESTRequest.ContentType
的方法是通过以下方式添加正文文本:
RESTRequest.Body.Add(AJSONString, ctAPPLICATION_JSON);