放一个TRESTClient,TRESTRequest然后TRESTRepsonse放到表单上。
设置RESTClient.BaseURL,RESTRequest.Method并且Resource,还添加了"Content-Type"头参数具有的价值"application/json"。
使用添加JSON字符串RESTRequest.AddBody,然后查看RESTRequest.ContentType。
它显示ctAPPLICATION_X_WWW_FORM_URLENCODED而不是ctAPPLICATION_JSON。这将导致服务器在RESTRequest.Execute()运行时返回错误。
无法将属性分配给该属性时,如何强制该请求使用正确的内容类型?
查看REST.Client源代码后,如果您使用以下命令指定内容类型:
AParameter := RESTRequest.Params.AddItem;
AParameter.ContentType := ctAPPLICATION_JSON;
AParameter.name := 'Content-Type';
AParameter.Value := 'application/json';
Run Code Online (Sandbox Code Playgroud)
代替这个:
RESTRequest.Params.AddHeader('Content-Type', 'application/json');
Run Code Online (Sandbox Code Playgroud)
然后,该TRESTRequest.ContentType属性将返回正确的值,这是期间使用的值TRESTRequest.Execute。
TRESTRequest.ContentType使用主体时强制正确的另一种方法是通过以下方式添加主体文本:
RESTRequest.Body.Add(AJSONString, ctAPPLICATION_JSON);
Run Code Online (Sandbox Code Playgroud)