#1 2013-10-22 17:24:00

Serrg
Member
From: Canada
Registered: 2013-10-22
Posts: 6

Client only. Send JSON data via http Post. Receive and parse JSON.

My task sounds pretty simple though I have never worked with http before... so I am not even sure I am on the right forum... :-)

We have MSSQL database on the server. Server-side talking to database is implemented in php.

I have a Delphi (BDS2006) client that needs to exchange data with that MSSQL database. Our php developer send me a couple of php file names with JSON format that he expects. He explained to me that I need to send JSON data to the server via http post method.

Question: Is it something that can be easily done with Synopse classes?

If so - are there any demo projects that would demonstrate how to send http post request?

Thanks in advance!

Offline

#2 2013-10-22 20:51:20

Serrg
Member
From: Canada
Registered: 2013-10-22
Posts: 6

Re: Client only. Send JSON data via http Post. Receive and parse JSON.

OK. I read some more today...
To answer my own question - Yes, I can do simple http post requests from Delphi using Synopse Framework.
It is described in Demo# 13 - Standalone JSON SQL Server.

It is really simple comparing to the code I found yesterday which showed how to create post requests using Wininet API.

Now I need to find out how to form my JSON string.

As I understood I cannot use TSQLHttpClient on the client if I do not use Synopse on the server side.
I have to use THttpClientSocket and form my string with some JSON functions.

Please correct me if I am wrong.

Offline

#3 2013-10-23 05:59:56

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,544
Website

Re: Client only. Send JSON data via http Post. Receive and parse JSON.

Serrg, on client side you can serialize/deserialize any Delphi class/record/array using  ObjectToJSON / JSONToObject from mORMot.pas.  To send data from Delphi to PHP server you can use TWinHTTP from SynCrtSock.pas

Offline

#4 2013-10-23 15:11:39

Serrg
Member
From: Canada
Registered: 2013-10-22
Posts: 6

Re: Client only. Send JSON data via http Post. Receive and parse JSON.

Zdorovenki Buli MPV,

Demo #16 - "Execute SQL via services" has some good examples of how to use ObjectToJSON.

Now about TWinHttp vs. THttpClientSocket... Documentation says TWinHttp seems to be faster than THttpClientSocket.

Is there any sample code on how to work with TWinHttp. I didn't find any so far. I found only THttpClientSocket in Demo #13.

Offline

#5 2013-10-23 16:27:04

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,544
Website

Re: Client only. Send JSON data via http Post. Receive and parse JSON.

See comments in TWinHttpAPI declaration. Class is VERY simple - just one method - Request. Something like this pseudocode:
 

  FClient := TWinHttp.Create(Server, Port, False);
.....
  Result := FClient.Request('/yourPHPServerMethod?param=value', 'GET', 0, Headers, ObjectToJSON(Request), 'application/json', FHeader, FData);
  Success := (Result = hscOK) and (FData <> '');
  JSONToObject(YourResultObject, Pointer(FData), FValid);
  

Offline

#6 2017-03-03 03:19:09

Ericshen
Member
Registered: 2017-02-25
Posts: 10

Re: Client only. Send JSON data via http Post. Receive and parse JSON.

I Just use the http post function ,but there is a error : winhttp.dll error 12030.

compile in delphi 10.1,  win 10.

Url := Format(https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s',[aToken])

procedure testpost(URL: string);
var
  fclient : TWinHTTP;
  URI : TURI;
  aToken,MenuJson : string;
  FHeader, FData  :SockString;
begin

  MenuJson :=
  '{"button":[{"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC"},{"name":"菜单","sub_button":[{"type":"view","name":"搜索","url":"http://www.soso.com/"},'+
  '{"type":"view","name":"视频","url":"http://v.qq.com/"},{"type":"click","name":"赞一下我们","key":"V1001_GOOD"}]}]}';

  URI.From(URL);

  FClient := TWinHttp.Create(URI.Server, URI.Port, False);

  FClient.Request(URL, 'POST', 0, '', MenuJson, 'application/json', FHeader, FData);
end;

Offline

#7 2017-03-03 03:49:18

Ericshen
Member
Registered: 2017-02-25
Posts: 10

Re: Client only. Send JSON data via http Post. Receive and parse JSON.

Thanks mpv , I use TWinHttpAPI class for  fclient  is OK.

Offline

#8 2017-03-03 08:24:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,240
Website

Re: Client only. Send JSON data via http Post. Receive and parse JSON.

You should better use an explicit conversion to UTF-8 encoding, via StringToUTF8(MenuJson).
Here your code makes an implicit conversion (and a compiler warning I suspect), sending the data with current AnsiString code page, which depends on the system it works on.

And you perhaps need to use URI.Address as first parameter of Request().

Offline

Board footer

Powered by FluxBB