How to call REST server with JSON


(Smokoveck Florencio) #1

how to call a REST server with JSON (without RO) with parameters (ComplexType) using the remobjects (client RO) in delphi

Is there an example?

For example, I have a third party service that receives a record (name, address, age, sex) in JSON and I want to send and also receive a data structure (code, message) in response.

(EvgenyK) #2

if you review the MegaDemo sample, you can see that client sends


and receives


so your non-RO server should accept above RO request.

Note: you can modify prepared JSON request in OnFinalizeMessage event via RootObject.

(Smokoveck Florencio) #3

But how do you call the method? Because with soap we have the wsdl to write the interface and call (ROServerRequest as IServiceTest) .Sum (x + y), but with JSON we do not have the service interface.

Megademo also uses the service interface developed with Remobjects.

could you make a small test (delphi) consumed a JSON service? it offers a service of ZIP in the following url: that returns the following structure:

“cep”: “01001-000”,
“logradouro”: “Praça da Sé”,
“complemento”: “lado ímpar”,
“bairro”: “Sé”,
“localidade”: “São Paulo”,
“uf”: “SP”,
“unidade”: “”,
“ibge”: “3550308”,
“gia”: “1004”

Best Regards,

(EvgenyK) #5

RO client uses POST method for communication with RO services

in your case, GET method should be used.

I can recommend to use native Indy (TIdHTTP) or Synapse (THTTPSend) classes for sending GET request.
it can be like IdHTTP.Get(..) or HTTPSend.HTTPMethod('GET',..)

Create JSON request you can with classes from uROJSONParser.pas unit

(Smokoveck Florencio) #6

Right, I understood, but my example was not very good!

I want to call methods with POST for no-RO services, what would the call look like? if I do not have the service interface.

(Smokoveck Florencio) #8

Another question about the JSON message:

The RO server expects that in the body of the message has the method it will be calling, is this a JSON mensen pattern or is it a RO standard to work on?

My test was as follows: I have a no-RO client to consume an RO service. I used MegaDemoServer and as a PostMan Client-Client ( I made a POST call passing the same message generated by MegaDemoClient and I’m not getting it to work.

(EvgenyK) #9

this is RO standard

I’ve tried with CURL and it was called correctly:

Y:\>curl -verbose "http://localhost:8099/json" -H  "accept: application/json" -H  "content-type: application/json" -d "{\"version\":\"1.1\",\"method\":\"MegaDemoService.EchoPerson\",\"params\":{\"aPerson\":{\"Age\":33,\"FirstName\":\"John\",\"LastName\":\"Smith\",\"Sex\":\"sxMale\"}}}"
*   Trying
* Connected to localhost ( port 8099 (#0)
> POST /json HTTP/1.1
> Host: localhost:8099
> User-Agent: curl/7.56.0
> Referer: rbose
> accept: application/json
> content-type: application/json
> Content-Length: 140
* upload completely sent off: 140 out of 140 bytes
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: application/json; charset=utf-8
< Content-Length: 108
< Date: Fri, 22 Feb 2019 08:46:01 GMT
< Accept-Encoding: gzip, identity
< Access-Control-Allow-Origin: *
* Connection #0 to host localhost left intact

(Smokoveck Florencio) #10

How is a standard JSON RO, how do I for my customer working with JSON, send a JSON in RO standard? If it’s a RO-specific JSON this makes it difficult to integrate with a customer who does not know RO, do not you think?

(EvgenyK) #11

RO request:

// some data here

RO response:

// some data here

You can accept any JSON requests from client and transform them into RO compatible JSON request in the OnCustomResponseEvent event.
see more details at Using OnCustomResponseEvent in a ROSDK Server snippet