delphi - IdHTTP 如何发送 x-www-form-urlencoded 正文
问题描述
我已经在 PostMan 中测试了 POST 函数,以使用以下正文参数执行 POST 函数:
这是 eBay 的此功能的文档:
HTTP method: POST
URL (Sandbox): https://api.sandbox.ebay.com/identity/v1/oauth2/token
HTTP headers:
Content-Type = application/x-www-form-urlencoded
Authorization = Basic <B64-encoded-oauth-credentials>
Request body:
grant_type=authorization_code
code=<authorization-code-value>
redirect_uri=<RuName-value>
我的第一次尝试如下:
function udxEBayExchangeAuthCodeForUserToken(AAuthCode: String; AIsProduction: Boolean): String;
var
xRequestBody: TStringStream;
begin
with objHTTP.Request do begin
CustomHeaders.Clear;
ContentType := 'application/x-www-form-urlencoded';
CustomHeaders.Values['Authorization'] := 'Basic ' + 'V2VpbmluZ0MtV2VpbmluZ0M';
end;
xRequestBody := TStringStream.Create('grant_type=' + 'authorization_code' + ','
+ 'code=' + 'v%5E1.1%23i%5E1%23f%5E0%23r%5E1%23I%5E3%23p%5E3' + ','
+ 'redirect_uri=' + 'myredirecturlnameinsandbox',
TEncoding.UTF8);
try
try
Result := objHTTP.Post('https://api.sandbox.ebay.com/identity/v1/oauth2/token', xRequestBody);
except
on E: Exception do
ShowMessage('Error on request: ' + #13#10 + e.Message);
end;
finally
xRequestBody.Free;
end;
end;
第二次尝试使用下面的 Body 代码
xRequestBody := TStringStream.Create('grant_type=' + 'authorization_code' + '&'
+ 'code=' + AAuthCode + '&'
+ 'redirect_uri=' + gsRuName,
TEncoding.UTF8);
两次尝试都给出 HTTP/1.1 400 错误请求。
我在 Stack Overflow 中进行了一些搜索,这是最接近的问题。唯一不同的部分是 POST 的正文。
谁能告诉我分配 POST 身体部位的正确方法是什么?谢谢。
解决方案
发送请求的首选方法是使用以 a作为输入的重载方法。您没有以正确的格式发送数据。application/x-www-form-urlencoded
TIdHTTP
TIdHTTP.Post()
TStrings
TStringStream
application/x-www-form-urlencoded
您无需使用该TIdHTTP.Request.CustomHeaders
属性来设置Basic
授权。 TIdHTTP
内置支持Basic
,只需根据需要使用TIdHTTP.Request.UserName
和TIdHTTP.Request.Password
属性,并将TIdHTTP.Request.BasicAuthentication
属性设置为true。
试试这个:
function udxEBayExchangeAuthCodeForUserToken(AAuthCode: String; AIsProduction: Boolean): String;
var
xRequestBody: TStringList;
begin
with objHTTP.Request do
begin
Clear;
ContentType := 'application/x-www-form-urlencoded';
BasicAuthentication := True;
UserName := ...;
Password := ...;
end;
xRequestBody := TStringList.Create;
try
xRequestBody.Add('grant_type=' + 'authorization_code');
xRequestBody.Add('code=' + AAuthCode);
xRequestBody.Add('redirect_uri=' + 'myredirecturlnameinsandbox');
try
Result := objHTTP.Post('https://api.sandbox.ebay.com/identity/v1/oauth2/token', xRequestBody);
except
on E: Exception do
ShowMessage('Error on request: ' + #13#10 + e.Message);
end;
finally
xRequestBody.Free;
end;
end;
如果你想发送你自己的TStringStream
,试试这个:
function udxEBayExchangeAuthCodeForUserToken(AAuthCode: String; AIsProduction: Boolean): String;
var
xRequestBody: TStringStream;
begin
with objHTTP.Request do
begin
Clear;
ContentType := 'application/x-www-form-urlencoded';
BasicAuthentication := True;
UserName := ...;
Password := ...;
end;
xRequestBody := TStringStream.Create('grant_type=' + 'authorization_code' + '&'
+ 'code=' + TIdURI.ParamsEncode(AAuthCode){'v%5E1.1%23i%5E1%23f%5E0%23r%5E1%23I%5E3%23p%5E3'} + '&'
+ 'redirect_uri=' + 'myredirecturlnameinsandbox',
TEncoding.UTF8);
try
try
xRequestBody.Position := 0;
Result := objHTTP.Post('https://api.sandbox.ebay.com/identity/v1/oauth2/token', xRequestBody);
except
on E: Exception do
ShowMessage('Error on request: ' + #13#10 + e.Message);
end;
finally
xRequestBody.Free;
end;
end;
推荐阅读
- prometheus-alertmanager - 已停止容器的说明
- visual-studio-code - 如何正确将 Eclipse Java 项目导入 VSCode
- postgresql - 为什么 Hasura 建议一对一的“数组关系”(即应该是对象关系)
- python - 熊猫数据框日期时间过滤器不起作用
- google-colaboratory - 我一直在试图弄清楚为什么这组编码在我尝试运行它时给了我数字“400”,谁能解释我做错了什么
- firebase - 有问题的小部件是: Uploader Build 函数绝不能返回 null
- android-studio - 我可以有几个 XML 布局文件并且只有一个主类吗?
- c++ - push_back 上的 std::vector 访问冲突
- c++ - 如何使用概念检查 std::less 是否有专门化?
- powerbi - 同步切片器问题