TIdHttp不工作方法开机自检-Delphi

50pmv0ei  于 2022-10-16  发布在  其他
关注(0)|答案(2)|浏览(185)

当我尝试使用POST方法发出请求时遇到问题,当Body中没有信息时,我如何使用没有参数/Body的POST?
我试图传递空值,但下面的代码返回以下错误:
HTTP/1.1 500

try
  responseres := tstringlist.Create;
  retorno := TstringStream.Create;
  params := tstringlist.Create;
  //--------------------------------
  IdHttp1.Request.CustomHeaders.Values['Authorization']     := 'Bearer ' + txtAccessToken.Text;
  IdHttp1.Request.CustomHeaders.Values['x-integration-key'] := 'GYBtKWR5QjSwVxXXXxxXxXeUeOsUe0nOuc8HyTnyT1s';
  //--------------------------------
  IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
  IdHTTP1.Request.Charset     := 'utf-8';
  //--------------------------------
  IdHTTP1.Post('https://api.onvio.com.br/dominio/integration/v1/activation/enable', params, retorno);
  JsonResponse        := TJSONObject.ParseJsonValue(UTF8Decode(retorno.DataString)) as TJSONObject;
  IntegrationKey.Text := JsonResponse.GetValue<string>('integrationKey');

  MemoJson.Lines.Text := responseres.Text;
finally
  params.Free;
  responseres.Free;
end;

在《 Postman 》中,这句话很管用:

ddarikpa

ddarikpa1#

多亏了你们的帮助**@雷米·勒博**,@罗伯特@戴夫·诺蒂奇,我设法解决了这个问题。太感谢你们了!

解决方案:

retorno     := TstringStream.Create;
 params      := nil;
 responseres := TStringlist.Create;

 IdHttp1.Request.CustomHeaders.Clear;
 IdHttp1.Request.CustomHeaders.Values['Authorization']     := 'Bearer ' + txtAccessToken.Text;
 IdHttp1.Request.CustomHeaders.Values['x-integration-key'] := 'GYBtKWR5QjSwVxXXXxxXxXeUeOsUe0nOuc8HyTnyT1s';
 IdHttp1.Request.CustomHeaders.Values['Content-Length']    := '0';
 IdHttp1.Request.CustomHeaders.Values['Accept']            := '*/*';
 IdHttp1.Request.CustomHeaders.Values['Accept-Encoding']   := 'gzip, deflate, br';
 IdHttp1.Request.CustomHeaders.Values['Connection']        := 'keep-alive';

 IdHTTP1.Request.ContentType       := 'application/json';
 IdHTTP1.Request.Charset           := 'utf-8';

 IdHTTP1.Post('https://api.onvio.com.br/dominio/integration/v1/activation/enable', params, retorno);
 JsonResponse        := TJSONObject.ParseJsonValue(UTF8Decode(retorno.DataString)) as TJSONObject;
 IntegrationKey.Text := JsonResponse.GetValue<string>('integrationKey');

 MemoJson.Lines.Text := FormatJSON(retorno.DataString);
 JsonToDataset(FDMemTable1, '['+retorno.DataString+']');
kdfy810k

kdfy810k2#

使用nil作为Body:Params:=nil;//不使用tstringlist。Create;
此外,您还需要释放“retorno”和“JsonResponse”对象。

相关问题