Randall Carpenter 1 Posted November 21, 2022 I am working on creating a API client using Delphi pascal. I have been unable to get it accept my POST request. I continue to get a 400 Bad Request error. The owners of the API server have indicated the C code shown below properly works. Unfortunately I am not a C programmer. Would someone be willing to translate the following C code to Delphi pascal. I would tremendously appreciate it. CURL *hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST"); curl_easy_setopt(hnd, CURLOPT_URL, "https://demo.checkbook.io/v3/check/digital"); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "accept: application/json"); headers = curl_slist_append(headers, "content-type: application/json"); headers = curl_slist_append(headers, "Authorization: XXXXX"); curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"recipient\":\"testing@checkbook.io\",\"name\":\"Widgets Inc.\",\"amount\":5,\"description\":\"Test Payment\"}"); CURLcode ret = curl_easy_perform(hnd); Thank you, Randall H. Carpenter Share this post Link to post
David Heffernan 2345 Posted November 21, 2022 You don't need to be a C programmer to translate this. You just need to understand the basics of curl. Do you have a Delphi curl library to hand? Share this post Link to post
Randall Carpenter 1 Posted November 21, 2022 I do not have a curl library. I have never used curl before. I (along with one other developer) have written other API clients but have never encountered this. Share this post Link to post
David Heffernan 2345 Posted November 21, 2022 How are you expecting anybody to translate this without there being an agreed curl library. Do you know what curl is? Share this post Link to post
Dave Nottage 557 Posted November 21, 2022 6 hours ago, Randall Carpenter said: I am working on creating a API client using Delphi pascal. I have been unable to get it accept my POST request. Posting the Delphi code that you are using might be helpful Share this post Link to post
Virgo 18 Posted November 22, 2022 12 hours ago, David Heffernan said: You don't need to be a C programmer to translate this. You just need to understand the basics of curl. Do you have a Delphi curl library to hand? You do not need event that. The code is completely obvious even without knowing curl. http request is sent with three additional headers set: accept, content-type and Authorization. And json is the request body. without seeing problem Delphi code, it is hard to know, what is done wrong in Delphi. Share this post Link to post
Virgo 18 Posted November 22, 2022 Sample for doing same thing with Synapse THTTPSend: procedure TestPost; var H: THTTPSend; S: UTF8String; Res: Boolean; begin H := THTTPSend.Create; try S := '{"recipient":"testing@checkbook.io","name":"Widgets Inc.","amount":5,"description":"Test Payment"}'; H.MimeType := 'application/json'; //synapse has separate propery for content-type header H.Headers.Add('accept: application/json'); H.Headers.Add('Authorization: XXXXX'); H.Document.Write(S[1], Length(S)); Res := H.HTTPMethod('POST', 'https://demo.checkbook.io/v3/check/digital'); if Res then begin SetLength(S, H.Document.Size); if H.Document.Size > 0 then H.Document.Read(S[1], H.Document.Size); ShowMessage(format('%d: %s', [H.ResultCode, S])); end else ShowMessage(H.Sock.LastErrorDesc); finally H.Free; end; end; ends with 400: {"error":"Invalid authorization header"} Because sample XXXXX authorization header is not correct obviously.... 1 Share this post Link to post
David Schwartz 426 Posted November 22, 2022 You don't need curl for this. RESTRequest and RESTResponse should be fine. The example above is similar to what you'd need. Share this post Link to post
Virgo 18 Posted November 23, 2022 Indeed. And it is quite likely, that actual problem is invalid JSON. Besides strings with quotes in them additional possible problem is, if decimal separator is not dot. Share this post Link to post