Working with JSON

Topic Specific Tutorials and Tips.

Moderator: Rathinagiri

Post Reply
User avatar
zolysoftsolutions
Posts: 106
Joined: Wed Feb 29, 2012 3:33 am
Location: Gyulakuta, Erdélyország
Contact:

Working with JSON

Post by zolysoftsolutions » Fri Mar 09, 2018 4:02 pm

Hi my friends,

I try to communicate between my local mysql database and a web application.
How can I send JSON to api and how can I recieve JSON from api?
Thank you in advance.

Regards,
Zoli B.
_______________________________
Open eyes for you!

edk
Posts: 362
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 106 times
Been thanked: 262 times

Post by edk » Fri Mar 09, 2018 5:59 pm

Hi Zoli.
In most cases, you send a JSON string with HTTP POST.
Some APIs may require setting the appropriate HTTP header.

Here is an example of simple communication with the Mandrill API:

Code: Select all

/* hb_json* samples:
https://github.com/Petewg/harbour-core/wiki/hb_J_K
https://groups.google.com/forum/#!topic/harbour-users/jl5N5DCNgxg
https://groups.google.com/forum/#!topic/harbour-users/nvLkUwlS9xw
*/

#include "hmg.ch"

Function Main()

Mandrill_()

Return Nil

****************************************************************************************
Function Mandrill_()

Local cURL, cApiUrl := "https://mandrillapp.com/api/1.0"
Local cAPIKey:="SomeAPIKey"
Local cPOSTdata
LOCAL h, oMandrill, hResp


//Init
BEGIN SEQUENCE WITH {|o| break(o)}
	oMandrill := Win_OleCreateObject( "MSXML2.ServerXMLHTTP" )

RECOVER
     MsgStop( "Microsoft XML Core Services (MSXML) 6.0 is not installed."+CRLF+;
          "Download and install MSXML 6.0 from http://msdn.microsoft.com/xml"+CRLF+;
          "before continuing.")
     oMandrill:=""

END SEQUENCE

IF EMPTY(oMandrill)
	MsgStop("Error while init.")
	RETURN 
ENDIF

//ping
//https://mandrillapp.com/api/docs/users.JSON.html#method=ping2
MsgInfo('Prepare for Ping')

cURL := cApiUrl + "/users/ping2.json"
h := { => }    
h [ "key" ] := cAPIKey
cPOSTdata := hb_jsonEncode( h , .T. )		//send as JSON
cResp := SendMandrill( cUrl, cPOSTdata, oMandrill )
	
IF cResp = "!ERROR!"
	MsgStop( cResp )	/* Report any errors */
ELSE
	MsgInfo( cResp , 'Response from server as JSON string')
	hResp := hb_jsonDecode( cResp )
	MsgDebug( hResp )
	cStatus     := hb_HGet( hResp  , "status" )
	nCode       := hb_HGet( hResp  , "code" )
	cName       := hb_HGet( hResp  , "name" )
	cMessage    := hb_HGet( hResp  , "message" )
	MsgDebug( cStatus , nCode , cName , cMessage )

ENDIF

//Close
oMandrill:Abort()


RETURN 

*****************************************************************************************
Function SendMandrill (cUrl, cPOSTdata, oMandrill)
Local cReturn

BEGIN SEQUENCE WITH {|o| break(o)}
	oMandrill:Open( "POST", cUrl, .F. )
	oMandrill:setRequestHeader("User-Agent", "Mandrill-Curl/1.0")
	oMandrill:Send( cPOSTdata )
	cReturn := oMandrill:ResponseBody()

RECOVER USING oErr
	cReturn := "!ERROR!" + CRLF + oErr:Description
 
END SEQUENCE
 	
RETURN cReturn
*****************************************************************************************

Edward.

User avatar
zolysoftsolutions
Posts: 106
Joined: Wed Feb 29, 2012 3:33 am
Location: Gyulakuta, Erdélyország
Contact:

Post by zolysoftsolutions » Fri Mar 09, 2018 6:18 pm

Thank you very much Edward.
_______________________________
Open eyes for you!

Post Reply