首页 > 解决方案 > 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 的正文。

IdHTTP 如何发送原始正文

谁能告诉我分配 POST 身体部位的正确方法是什么?谢谢。

标签: delphihttp-postebay-apiidhttpx-www-form-urlencoded

解决方案


发送请求的首选方法是使用以 a作为输入的重载方法。您没有以正确的格式发送数据。application/x-www-form-urlencodedTIdHTTPTIdHTTP.Post()TStringsTStringStreamapplication/x-www-form-urlencoded

您无需使用该TIdHTTP.Request.CustomHeaders属性来设置Basic授权。 TIdHTTP内置支持Basic,只需根据需要使用TIdHTTP.Request.UserNameTIdHTTP.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;

推荐阅读