Title: POST json parameter Post by: Bryn Lewis 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); 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 Title: Re: POST json parameter Post by: D.Tkalcec (RTC) 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); Here is another Forum topic with a similar question (How to POST a request) (https://rtcforum.teppi.net/index.php?topic=334.0). 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 Title: Re: POST json parameter Post by: Bryn Lewis on February 08, 2016, 11:48:11 PM Yep, that works.
thanks, Bryn |