[英]How to set body data indy HTTPS post
So I've looked around, and the only question describing my problem is 6 years old with 0 answers, so I guess I will try again. 所以我环顾四周,唯一描述我的问题的问题是6岁零答案,所以我想我会再试一次。
I am using delphi 2009 with Indy10. 我正在Indel10中使用delphi 2009。
I am trying to post JSON
to an api using HTTPS. 我正在尝试使用HTTPS将
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 }' );
I have seen many answers suggesting that the JSON
payload should be given as a string
param in the TidHTTP.Post
method, but when i try that, it expects a filepath, and throws an error saying: 我已经看到很多答案,建议在
TidHTTP.Post
方法中将JSON
有效负载作为string
参数给出,但是当我尝试这样做时,它期望一个文件路径,并抛出一条错误消息:
'Cannot open file "[path to project{JSONName:JSONValue }]".
'无法打开文件“ [项目的路径{JSONName:JSONValue}]”。 The specified file was not found'.
指定的文件未找到'。
If i add my JSON
to a TStringList
and add give that as a parameter, it simply adds the JSON to the header of the request. 如果我将
JSON
添加到TStringList
并添加TStringList
作为参数,则它将JSON添加到请求的标头中。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
The Post
overload that takes a second string indeed interprets it as a filename: 确实需要第二个字符串的
Post
重载确实将其解释为文件名:
function Post(AURL: string; const ASourceFile: String): string; overload;
That's why this doesn't work. 这就是为什么这不起作用。 You need to instead use the overload that takes a
TStream
: 您需要改为使用需要
TStream
的重载:
function Post(AURL: string; ASource: TStream): string; overload;
You can put your JSON in a TStringStream
: 您可以将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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.