如何设置 body 数据 indy HTTPS post
How to set body data indy HTTPS post
所以我环顾四周,唯一描述我的问题的问题是 6 年前的 0 个答案,所以我想我会再试一次。
我正在使用 delphi 2009 和 Indy10。
我正在尝试使用 HTTPS post JSON
到 api。
Instance.FHTTP := TIdHTTP.Create;
Instance.FHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(Instance.FHTTP);
{$IFDEF DEBUG}
Instance.FHTTP.ProxyParams.ProxyPort := 8888;
Instance.FHTTP.ProxyParams.ProxyServer := '127.0.0.1';
{$ENDIF}
Instance.FHTTP.Request.ContentType := 'application/json';
Instance.FAccessToken := Instance.FHTTP.Post('https://somedomain.com/api/endpoint', '{JSONName: JSONValue }' );
我看到很多答案建议 JSON
有效载荷应该作为 TidHTTP.Post
方法中的 string
参数给出,但是当我尝试这样做时,它需要一个文件路径,并抛出一条错误消息:
'Cannot open file "[path to project{JSONName:JSONValue }]". The specified file was not found'.
如果我将 JSON
添加到 TStringList
并将其添加为参数,它只是将 JSON 添加到请求的 header。
非常感谢任何帮助。
采用第二个字符串的 Post
重载确实将其解释为文件名:
function Post(AURL: string; const ASourceFile: String): string; overload;
这就是为什么这不起作用。您需要改用采用 TStream
:
的重载
function Post(AURL: string; ASource: TStream): string; overload;
你可以把你的 JSON 放在 TStringStream
:
StringStream := TStringStream.Create('{JSONName: JSONValue }', TEncoding.UTF8);
try
Instance.FAccessToken := Instance.FHTTP.Post('https://somedomain.com/api/endpoint', StringStream);
finally
StringStream.Free;
end;
所以我环顾四周,唯一描述我的问题的问题是 6 年前的 0 个答案,所以我想我会再试一次。
我正在使用 delphi 2009 和 Indy10。
我正在尝试使用 HTTPS post JSON
到 api。
Instance.FHTTP := TIdHTTP.Create;
Instance.FHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(Instance.FHTTP);
{$IFDEF DEBUG}
Instance.FHTTP.ProxyParams.ProxyPort := 8888;
Instance.FHTTP.ProxyParams.ProxyServer := '127.0.0.1';
{$ENDIF}
Instance.FHTTP.Request.ContentType := 'application/json';
Instance.FAccessToken := Instance.FHTTP.Post('https://somedomain.com/api/endpoint', '{JSONName: JSONValue }' );
我看到很多答案建议 JSON
有效载荷应该作为 TidHTTP.Post
方法中的 string
参数给出,但是当我尝试这样做时,它需要一个文件路径,并抛出一条错误消息:
'Cannot open file "[path to project{JSONName:JSONValue }]". The specified file was not found'.
如果我将 JSON
添加到 TStringList
并将其添加为参数,它只是将 JSON 添加到请求的 header。
非常感谢任何帮助。
采用第二个字符串的 Post
重载确实将其解释为文件名:
function Post(AURL: string; const ASourceFile: String): string; overload;
这就是为什么这不起作用。您需要改用采用 TStream
:
function Post(AURL: string; ASource: TStream): string; overload;
你可以把你的 JSON 放在 TStringStream
:
StringStream := TStringStream.Create('{JSONName: JSONValue }', TEncoding.UTF8);
try
Instance.FAccessToken := Instance.FHTTP.Post('https://somedomain.com/api/endpoint', StringStream);
finally
StringStream.Free;
end;