title: IdHTTP how to send x-www-form-urlencoded body
body: <p>I have tested POST function in PostMan to do POST function with body parameters as below:</p>
  
  <p><a href="https://i.stack.imgur.com/AdEI2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AdEI2.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/nJb77.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nJb77.png" alt="enter image description here"></a></p>
  
  <p>Here is eBay's document for this function:</p>
  
  <pre><code>  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 &lt;B64-encoded-oauth-credentials&gt;
  
    Request body:
      grant_type=authorization_code
      code=&lt;authorization-code-value&gt;
      redirect_uri=&lt;RuName-value&gt;
  </code></pre>
  
  <p>My first attempt was as follow:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Second attempt tried with below code for Body</p>
  
  <pre><code>  xRequestBody := TStringStream.Create('grant_type=' + 'authorization_code' + '&amp;'
                                       + 'code=' + AAuthCode + '&amp;'
                                       + 'redirect_uri=' + gsRuName,
                                          TEncoding.UTF8);
  </code></pre>
  
  <p>Both attempts give HTTP/1.1 400 Bad Request.</p>
  
  <p>I have done some searching in Stack Overflow, and this is the closest question. The only different part is body of POST.</p>
  
  <p><a href="https://stackoverflow.com/questions/44665349/idhttp-how-to-send-raw-body">IdHTTP how to send raw body</a></p>
  
  <p>Can anyone please advise me what is correct way to assign POST body part?
  Thanks.</p>