Search code examples
delphicurlhttpsindy

How to use DeepL API curl command with Indy TidHTTP?


Example request to https://www.deepl.com:

curl https://api-free.deepl.com/v2/translate \
 -d auth_key=[yourAuthKey] \
 -d "text=Hello world!" \
 -d "target_lang=DE"

Example response:

{"translations": [{
                   "detected_source_language":"EN",
                   "text":"Hallo Welt!"
                 }]
}

I want to implement the above example in Delphi 11 using Indy's TIdHTTP component, but I have not been able to translate the existing answers into a workable solution for me.


Solution

  • The TIdHTTP equivalent of that curl example is to pass a TStrings object containing name=value pairs to the TIdHTTP.Post() method, eg:

    PostData := TStringList.Create;
    try
      PostData.Add('auth_key=[yourAuthKey]');
      PostData.Add('text=Hello, world!');
      PostData.Add('target_lang=DE');
      Response := IdHTTP.Post('https://api-free.deepl.com/v2/translate', PostData);
    finally
      PostData.Free;
    end;