KodeZwerg 54 Posted April 18, 2021 good day, is someone willing to help me port those two c# snippets into working delphi (winapi/wininet) snippets please? the first one: var client = new HttpClient(); var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri("https://yandextranslatezakutynskyv1.p.rapidapi.com/translate"), Headers = { { "x-rapidapi-key", "1234567890" }, { "x-rapidapi-host", "YandexTranslatezakutynskyV1.p.rapidapi.com" }, }, Content = new FormUrlEncodedContent(new Dictionary<string, string> { { "apiKey", "1234567890" }, { "lang", "de-en" }, { "text", "dieser text soll übersetzt werden" }, }), }; using (var response = await client.SendAsync(request)) { response.EnsureSuccessStatusCode(); var body = await response.Content.ReadAsStringAsync(); Console.WriteLine(body); } and the second one: var client = new HttpClient(); var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri("https://systran-systran-platform-for-language-processing-v1.p.rapidapi.com/translation/text/translate?source=auto&target=en&input=dieser%20text%20soll%20%C3%BCbersetzt%20werden"), Headers = { { "x-rapidapi-key", "1234567890" }, { "x-rapidapi-host", "systran-systran-platform-for-language-processing-v1.p.rapidapi.com" }, }, }; using (var response = await client.SendAsync(request)) { response.EnsureSuccessStatusCode(); var body = await response.Content.ReadAsStringAsync(); Console.WriteLine(body); } thank you in advance! Share this post Link to post
KodeZwerg 54 Posted April 19, 2021 function Http_Get(const Server: string; const Resource: string; const key: string): string; const headerkey = 'x-rapidapi-key: '; headerhost = 'x-rapidapi-host: systran-systran-platform-for-language-processing-v1.p.rapidapi.com'; var hInet: HINTERNET; hURL: HINTERNET; Buffer: array[0..1023] of AnsiChar; i, BufferLen: cardinal; begin result := ''; hInet := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); try hURL := InternetOpenUrl(hInet, PChar('https://' + Server + Resource), {how to put header in here?}nil, 0, INTERNET_FLAG_SECURE , 0); try repeat InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen); if BufferLen = SizeOf(Buffer) then result := result + AnsiString(Buffer) else if BufferLen > 0 then for i := 0 to BufferLen - 1 do result := result + Buffer[i]; until BufferLen = 0; finally InternetCloseHandle(hURL); end; finally InternetCloseHandle(hInet); end; end; i am somehow stuck... please need help Share this post Link to post
mvanrijnen 123 Posted April 19, 2021 start here 🙂 : REST.Client.TRESTClient - RAD Studio API Documentation (embarcadero.com) Share this post Link to post
KodeZwerg 54 Posted April 19, 2021 function Http_Get: string; const headerkey = 'x-rapidapi-key: 1234567890'; headerhost = 'x-rapidapi-host: systran-systran-platform-for-language-processing-v1.p.rapidapi.com'; var hInet: HINTERNET; hURL: HINTERNET; Buffer: array[0..1023] of AnsiChar; i, BufferLen: cardinal; begin result := ''; hInet := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); try hURL := InternetOpenUrl(hInet, PChar('https://systran-systran-platform-for-language-processing-v1.p.rapidapi.com/translation/text/translate?source=auto&target=en&input=dieser%20text%20soll%20%C3%BCbersetzt%20werden'), PChar(headerkey + headerhost), 0, INTERNET_FLAG_SECURE, 0); try repeat InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen); if BufferLen = SizeOf(Buffer) then result := result + AnsiString(Buffer) else if BufferLen > 0 then for i := 0 to BufferLen - 1 do result := result + Buffer[i]; until BufferLen = 0; finally InternetCloseHandle(hURL); end; finally InternetCloseHandle(hInet); end; end; this works, i get result = {"message":"You are not subscribed to this API."} now the code for post... thank you @mvanrijnen but i like to try with wininet. Share this post Link to post