Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何强制TRESTRequest.ContentType为ctAPPLICATION_JSON_Rest_Delphi - Fatal编程技术网

如何强制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);