Jump to content
Randall Carpenter

C to Delphi pascal

Recommended Posts

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

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
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
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

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....

  • Thanks 1

Share this post


Link to post

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×