我正在与Delphi进行的REST API调用进行对抗。我想将PUT请求发送到服务器,并将contenttype设置为application / json:
aCustomRestClient.BaseURL := domein;
aCustomRestRequest.Client := aCustomRestClient;
aCustomRestRequest.Response := aCustomRestResponse;
aCustomRestRequest.Method := rmPUT;
aCustomRestRequest.Resource := '/service/API/1501/allowanceCategory';
aCustomRestRequest.Params.AddHeader('sessionId', SessionToken);
qryAllowanceCategory.First;
o := TJSONObject.Create;
arr:=TJSONarray.create;
try
o.AddPair('createdBy',qryAllowanceCategory.fieldbyname('createdBy').AsString);
{lot more pairs here}
o.AddPair('percentage', TJSONnumber.create(qryAllowanceCategory.fieldbyname('percentage').AsFloat));
o.AddPair('remark', 'JUR '+ qryAllowanceCategory.fieldbyname('remark').AsString);
arr.add(o);
aCustomRestRequest.ClearBody;
aCustomRestResponse.contenttype:='application/json';
aCustomRestClient.contenttype:='application/json';
aCustomRestRequest.AddBody(arr.ToString);
aCustomRestRequest.Execute;
但是,当我查看随提琴手发送的请求时:
PUT http://shadow.jdvretail.com/service/API/1501/allowanceCategory HTTP/1.1
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded
Accept: application/json, text/plain; q=0.9, text/html;q=0.8,
Accept-Charset: utf-8, *;q=0.8
Authorization: Basic <<Authorization properly filled>>
User-Agent: Embarcadero RESTClient/1.0
sessionId: <<sessionID GUID properly filled here>>
Content-Length: 542
Content-type仍然是错误的...如何/在何处正确地将contentType设置为'application / json'?
[TRestClient.AddBody(<string>)
具有可选的第二个参数,允许设置内容类型。
procedure AddBody(const ABodyContent: string; AContentType: TRESTContentType = ctNone); overload;