How to call REST server with JSON

delphi

(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

{"version":"1.1",
 "method":"MegaDemoService.EchoPerson",
 "params":
     {"aPerson":
          {"Age":33,"FirstName":"John","LastName":"Smith","Sex":"sxMale"}
     }
}

and receives

{"version":"1.1",
 "result":
     {"anotherPerson":
        {"Age":33,"FirstName":"John","LastName":"Smith","Sex":"sxMale"}
     }
}

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? https://viacep.com.br/ it offers a service of ZIP in the following url: viacep.com.br/ws/01001000/json/ 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 (https://github.com/postmanlabs/postman-app-support/wiki). 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 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) 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: *
<
{"version":"1.1","result":{"anotherPerson":{"Age":33,"FirstName":"John","LastName":"Smith","Sex":"sxMale"}}}
* 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:

{"version":"1.1",
 "method":"service_name.method_name",
 "params":
     {
// some data here
     }
}

RO response:

{"version":"1.1",
 "result":
     {
// 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