httpdelphipostmultipartform-dataidhttp

I'm trying to send a POST to an API in Delphi but I get Error 400. Ho can I debug this?


I'm a Delphi programmer and this is the first time for me to deal with HTTP.

I'm trying to send a POST request to an API, but I get back a 400 BAD REQEST error.

This is the code I'm using:

FormData := TIdMultipartFormDataStream.Create;
try
  // Add the file to the form data
  FormData.AddFile('file', FilePath, 'application/json');  // Content type for JSON file

  FormData.AddFormField('input_file_id', File_ID);
  FormData.AddFormField('completion_window:','"24h"');

  // sets the params for the HTTP request
  idHTTP.Request.ContentType := 'multipart/form-data';
  APIKey := 'Bearer ' + MY_API_Key;
  idHTTP.Request.CustomHeaders.AddValue('Authorization', APIKey);

  Response := IdHTTP.Post(FinalURL, FormData);           // Eexec the POST request

  // parse the JSON string of the response
  JsonResponse := TJSONObject.ParseJSONValue(Response);
  try
    if JsonResponse <> nil then
    begin
      File_Upload_Response_Object.id := (JsonResponse.GetValue<string>('id')).ToInteger;
      // ... all the other params parsed ...
    end
    else
    begin
       File_Upload_Response_Object.ErrorCode := 1; // I send back an error of some type
    end;
  finally
    JsonResponse.Free;
  end;    
finally   
  FormData.Free;
  SSLHandler.Free;
  idHTTP.Free;
end;   

What am I doing wrong?

This is the content of the FHTTP.Proto thant seems to go into the "Result" of the post command:

($29A8C1E1820, '', $29A8C05F4A0, '', '', '', '', '', -1, -1, -1, -1, '', 'multipart/form-data', '', $29A8C05F680, 0, 0, '', 0, '', False, '', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', '', '', '', '', '', '', '', 'Mozilla/3.0 (compatible; Indy Library)', '', '', '', $29A8D98AED0, False, nil, '', $29A8C1E1820, '', '', nil, ctNormal, Id_IPv4, '')  

I really can't see anything useful here.


Solution

  • On this line:

    FormData.AddFormField('completion_window:','"24h"');
    

    You should not have a : on the key name. Also, are you sure the value should be quoted?

    Try this:

    FormData.AddFormField('completion_window','24h');