RTC Forums
November 01, 2024, 01:39:24 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: POST json parameter  (Read 5196 times)
Bryn Lewis
RTC Expired
*
Posts: 17


« on: February 08, 2016, 05:38:48 AM »

Hi

I am trying to implement this API, which has the following CURL example:

RECIPIENT_NUMBER="your number"
TOKEN="<access_token>"
 
curl -H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"to\":\"$RECIPIENT_NUMBER\", \"body\":\"Hello!\"}" \
"https://api.telstra.com/v1/sms/messages"

My code:
Code:
procedure TForm1.RtcDataRequest1BeginRequest(Sender: TRtcConnection);
begin
  rtcHttpClient1.Request.FileName:='/v1/sms/messages';
  //rtcHttpClient1.Request.FileName:='/v1/sms/messages/?{\"to\":\04xxxxxxxx, \"body\":\"Hello!\"}';
  //rtcHttpClient1.Request.FileName:='/v1/sms/messages/?{"to":"04xxxxxxxx", "body":"Hello!"}';

  rtcHttpClient1.Request.host:=  rtcHttpClient1.ServerAddr;

  rtcHttpClient1.write;
end;

procedure TForm1.RtcDataRequest1DataReceived(Sender: TRtcConnection);
begin
  if rtcHttpClient1.Response.Done then
  begin
    memo1.lines.clear;
    memo1.lines.add(rtcHttpClient1.Read);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  RtcDataRequest1.Request['Content-Type']:='application/json';
  RtcDataRequest1.Request['Authorization']:='Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXX';
  rtcHttpClient1.Connect;
  rtcDataRequest1.request.Method:='POST';
  rtcDataRequest1.post;
end;

I don't know where to put the post data, ie what to do when it is a json string without a parameter name?

With the above I get { "status": 400, "message": "Invalid or missing request parameters" }

With the second I get 'invalid URI'.

thanks, Bryn
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: February 08, 2016, 10:42:32 AM »

According to the CURL syntax, the "-d" parameter is HTTP content body, which you can send from the OnBeginRequest event by using the "Write" or "WriteEx" method. In other words, you should modify your "OnBeginRequest" event to look something like this:

procedure TForm1.RtcDataRequest1BeginRequest(Sender: TRtcConnection);
var
  Cli:TRtcDataClient absolute Sender;
  MyContentBody:String;
begin
  Cli.Request.FileName:='/v1/sms/messages';
  Cli.Request.Host:=  Cli.ServerAddr;
  MyContentBody:='"{\"to\":\"$RECIPIENT_NUMBER\", \"body\":\"Hello!\"}"';
  Cli.Write(MyContentBody);
end;

To make this event more generalized, since you are posting a short content and you can fill up eveything from the main thread, you can prepare all the request parameters in the main thread and use the Request.Info property to pass on the content body. For example, like this:

Code:
procedure TForm1.RtcDataRequest1BeginRequest(Sender: TRtcConnection);
var
  Cli:TRtcDataClient absolute Sender;
  MyContent:String;
begin
  MyContent:=Cli.Request.Info.asString['body'];
  Cli.Write(MyContent);
end;

procedure TForm1.RtcDataRequest1DataReceived(Sender: TRtcConnection);
var Cli:TRtcDataClient absolute Sender;
begin
  if Cli.Response.Done then
  begin
    memo1.lines.clear;
    memo1.lines.add(Cli.Read);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  RtcDataRequest1.Request.Host:=  rtcHttpClient1.ServerAddr;
  RtcDataRequest1.Request.Method:='POST';
  RtcDataRequest1.Request.FileName:='/v1/sms/messages';
  RtcDataRequest1.Request['Content-Type']:='application/json';
  RtcDataRequest1.Request['Authorization']:='Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXX';
  RtcDataRequest1.Request.Info.asString['body']:= '"{\"to\":\"$RECIPIENT_NUMBER\", \"body\":\"Hello!\"}"';
  rtcHttpClient1.Connect;
  RtcDataRequest1.Post;
end;

Here is another Forum topic with a similar question (How to POST a request).

PS. When posting "text" content, make sure to Encode it propperly, because the Write method always sends RAW data (8-bit characters), ignoring higher bits. For example, if your content is Unicode and the receiver expects the content to be UTF-8 encoded, you can use the Utf8Encode function for encoding content to UTF-8 before sending and the Utf8Decode function to decode the content received with UTF-8 encoding. This rule does not apply to JSON content, since it is already encoded into JSON format, which only uses 8-bit characters.

Best Regards,
Danijel Tkalcec
Logged
Bryn Lewis
RTC Expired
*
Posts: 17


« Reply #2 on: February 08, 2016, 11:48:11 PM »

Yep, that works.

thanks, Bryn
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 17 queries.