You are not logged in.
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
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
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
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
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
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
Thanks mpv , I use TWinHttpAPI class for fclient is OK.
Offline
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