Recent

Author Topic: Accessing REST API  (Read 2676 times)

Chris

  • New Member
  • *
  • Posts: 30
Accessing REST API
« on: December 05, 2021, 10:23:18 am »
Hi

I'm trying to access a REST API with the following code, but keep getting this response HttpMessageNotReadableError","code":400,"error_message":"Required request body is missing, and I can't work out why.

When I direct the URL to a socket testing program running locally it looks like the connection is being closed before the json data is sent.

Any help or suggestions would be gratefully received. 

procedure TForm1.Button2Click(Sender: TObject);
const url = 'https://api.textanywhere.com/API/v1.0/REST/sms';
var
  postJson: TJSONObject;
  responseData: String;
begin
  postJson := TJSONObject.Create;
  postJson.Add('message_type', 'GP');
  postJson.Add('message', 'Test message');
  postJson.Add('recipient','[+44]');
  postJson.Add('sender', 'XX');
  postJson.Add('scheduled_delivery_time', '20211223101010');
  postJson.Add('order_id', '12344');
  postJson.Add('returnCredits', true);
  With TFPHttpClient.Create(Nil) do
  try
    AddHeader('Content-Type', 'application/json');
    AddHeader('Connection', 'Keep-Alive');
    AddHeader('user_key',key);
    AddHeader('session_key',session);
    RequestBody := TStringStream.Create(postJson.AsJSON);
    responseData := Post(url);

  finally
   Free;

  end;
memo1.Lines.Add(responseData);
end;                           



paweld

  • Sr. Member
  • ****
  • Posts: 324
Re: Accessing REST API
« Reply #1 on: December 05, 2021, 10:51:29 am »
recipients you must pass as an array:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. const
  3.   url = 'https://api.textanywhere.com/API/v1.0/REST/sms';
  4. var
  5.   postJson: TJSONObject;
  6.   reciparr: TJSONArray;
  7.   responseData: String;
  8. begin
  9.   postJson := TJSONObject.Create;
  10.   postJson.Add('message_type', 'GP');
  11.   postJson.Add('message', 'Test message');
  12.   reciparr := TJSONArray.Create;
  13.   reciparr.Add('+44 phone number'); //add another phone number: reciparr.Add('another phone number');
  14.   postJson.Add('recipient', reciparr);              
  15.   postJson.Add('sender', 'XX');
  16.   postJson.Add('scheduled_delivery_time', '20211223101010');
  17.   postJson.Add('order_id', '12344');
  18.   postJson.Add('returnCredits', true);
  19.   with TFPHttpClient.Create(Nil) do
  20.   try
  21.     AddHeader('Content-Type', 'application/json');
  22.     AddHeader('Connection', 'Keep-Alive');
  23.     AddHeader('user_key', key);
  24.     AddHeader('session_key', session);
  25.     RequestBody := TStringStream.Create(postJson.AsJSON);
  26.     responseData := Post(url);
  27.   finally
  28.    Free;
  29.   end;
  30.   memo1.Lines.Add(responseData);
  31. end;
Best regards
paweld

Chris

  • New Member
  • *
  • Posts: 30
Re: Accessing REST API
« Reply #2 on: December 05, 2021, 11:41:02 am »
Thanks paweld, I just tried that but still get the same error.

 

TinyPortal © 2005-2018