Forum > Networking and Web Programming

Accessing REST API

(1/1)

Chris:
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:
recipients you must pass as an array:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button2Click(Sender: TObject);const   url = 'https://api.textanywhere.com/API/v1.0/REST/sms';var  postJson: TJSONObject;  reciparr: TJSONArray;  responseData: String;begin  postJson := TJSONObject.Create;  postJson.Add('message_type', 'GP');  postJson.Add('message', 'Test message');  reciparr := TJSONArray.Create;  reciparr.Add('+44 phone number'); //add another phone number: reciparr.Add('another phone number');  postJson.Add('recipient', reciparr);                 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;

Chris:
Thanks paweld, I just tried that but still get the same error.

Navigation

[0] Message Index

Go to full version