Page 1 of 1

Working TInternet post example

Posted: Fri May 24, 2019 4:35 pm
by ftwein
Tested with https://jsonplaceholder.typicode.com/

Code: Select all

METHOD Button2Click( oSender ) CLASS TForm1
LOCAL cTexto, cData, xerro, cJson
LOCAL oInternet, Connection, File
::oMemo1:Value:= ""
BEGIN SEQUENCE
   oInternet:= TInternet():New( Self )
   oInternet:nPort := INTERNET_DEFAULT_HTTPS_PORT
   oInternet:cAgent := "Panel2"
   oInternet:nTimeOut:= 60000
   IF !oInternet:Open()
      BREAK "Not Open"
   ENDIF
   Connection:= oInternet:Connect("jsonplaceholder.typicode.com")
   IF !Connection
      BREAK "Not Connection"
   ENDIF
   cJson:= '{ "title": "foo", "body": "bar", "userId": 1 }'
   File:= oInternet:OpenRequest(;
    "POST",;
    "/posts",;
    INTERNET_FLAG_SECURE,;
    "HTTP/1.0")
   IF !oInternet:SendRequest(;
      File,;
      "Content-type: application/json",;
      cJson )
      BREAK "Not SendRequest"
   ENDIF
   cTexto:=''
   WHILE oInternet:ReadFile( File, @cData, 65536 )
       cTexto += cData
   ENDDO
   ::oMemo1:Value:= cTexto + " *** "+oInternet:GetErrorDescription()

RECOVER USING xerro
   ::oMemo1:Value:= oInternet:GetErrorDescription()
   MsgInfo( xerro )
END SEQUENCE


Regards,

Fausto Di Creddo Trautwein