#1 2014-08-18 06:49:41

hkcity1111
Member
Registered: 2014-08-18
Posts: 14

Receive Json by Calculator Sample

Dear all

I am new first comer for mORMot. I want to ask how to post json to http interface service by Calculator by javascrpt.

The following is sample but it got { "ErrorCode":400, "ErrorText":"Bad Request" } by the following coding.
If I change data : [4,5] , then result will be return sucessfully.
How can I amend in mORMot contract/service to achieve result or any existing reference to this
Sorry for my stupid question.

Thanks a lot!

      var dataString = { n1 :4, n2 :5};
      $.ajax({ 
        type : 'POST',
        url: "http://localhost:888/"+SessionSign("root/Calculator.Add"), // JQuery loads 
	data : JSON.stringify(dataString),		
        dataType: 'json', 
        contentType : 'application/json',
        success: function(data) 
        {
          alert("url was signed OK");
        }
      });

Regards,
  HK City

Last edited by hkcity1111 (2014-08-18 07:01:43)

Offline

#2 2014-08-18 07:02:26

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

Re: Receive Json by Calculator Sample

Input should be a JSON array. This is the current design and implementation.
As an alternative (with the default TSQLRestRoutingREST mode), you can encode the parameters at URI level, e.g. root/Calculator.Add?n1=4&n2=5

We may change TServiceMethod.InternalExecute() to allow incoming parameters to be encoded as a JSON object, alternatively to a JSON array.
You could create a feature request ticket to implement this behavior, if you wish. See http://synopse.info/fossil/reportlist

Offline

#3 2014-08-18 07:27:19

hkcity1111
Member
Registered: 2014-08-18
Posts: 14

Re: Receive Json by Calculator Sample

Thanks for your prompt reply!

Offline

#4 2014-08-18 07:51:10

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

Re: Receive Json by Calculator Sample

I guess that using an array should not be an issue.

I suppose your service call will be wrapped within a function, so it is pretty easy to write something like this:

function calculatorAdd(n1,n2, onSuccess) {
      $.ajax({ 
        type : 'POST',
        url: "http://localhost:888/"+SessionSign("root/Calculator.Add"), // JQuery loads 
	data : JSON.stringify([n1,n2]),		
        dataType: 'json', 
        contentType : 'application/json',
        success: onSuccess
      });
}

In fact, this is what our SmartMobileStudio generated code wrappers do.

Note that for best performance, you may need to enable CORS and set contentType as 'text/plain' to avoid an unnecessary double request to the server from the AJAX client - see "preflight" at https://developer.mozilla.org/en-US/doc … ntrol_CORS
Our SynCrossPlatform*.pas units could be used as a reference implementation, especially in all its {$ifdef ISDWS} parts, for AJAX clients.

Offline

#5 2014-08-18 11:46:06

hkcity1111
Member
Registered: 2014-08-18
Posts: 14

Re: Receive Json by Calculator Sample

Thank for your advice and I use json array as input and use several json objects within array.
Previously I sometime use dot.net to create service because the coding is very short
and now I can use delphi mORMot and the coding is very short too.
Very good framework and useful to me. : )
Thanks!

Offline

#6 2014-08-18 12:16:59

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

Re: Receive Json by Calculator Sample

BTW, which code are you using for SessionSign()?
Sounds like if the code posted by RangerX some months ago had some minor issues.
If you have a more stable version, could you please share it?
It may help others using "pure JavaScript" mORMot clients, e.g. with jQuery.

Offline

#7 2014-08-21 01:59:14

hkcity1111
Member
Registered: 2014-08-18
Posts: 14

Re: Receive Json by Calculator Sample

I use weak authorization by "Javascript authentication" in the forum before.
If I got the fixing in javascript,  I will post here. Thanks!

Offline

#8 2014-08-23 14:55:03

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

Re: Receive Json by Calculator Sample

We have just implemented your request.
In fact, this feature request may be used directly in our MVC model: the incoming parameters of the controllers or the view may be defined as a JSON object, and TServiceMethod.InternalExecute() will use only the needed parameters.

The mORMot server will now accept the incoming parameters to be encoded as a JSON object of named values, in addition to the default (and more efficient) JSON array format.

For instance, the following "class request" for ICalculator.Add:

POST /root/Calculator.Add
(...)
[1,2]

Could alternatively be sent as such:

POST /root/Calculator.Add
(...)
{"n1":1,"n2":2}

Of course, order of the values is not mandatory in a JSON object, since parameters will be lookup by name. As a result, the following request will be the same as the previous one:

POST /root/Calculator.Add
(...)
{"n2":2,"n1":1}

For a sicClientDriven mode service, the needed instance ID is appended to the URI:

POST /root/ComplexNumber.Add/1234
(...)
{"aReal":20,"aImaginary":30}

In some cases, naming the parameters could be useful, on the client side. But this should not be the default, since it will be slightly slower (for parsing and checking the names), and use more bandwidth at transmission.

Any missing parameter in the incoming JSON object will be replaced by its default value. For instance, the following will run IComplexNumber(0,2):

POST /root/Calculator.Add
(...)
{"n2":2}

Any unknown parameter in the incoming JSON object will just be ignored. It could be handy, if you want to transmit some generic execution context (e.g. a global "data scope" in a MVC model), and let the service use only the values it needs.

POST /root/ComplexNumber.Add/1234
(...)
{"Session":"1234","aImaginary":30,"aReal":20,"UserLogged":"Nikita"}

Of course, the extra values would consume some bandwidth for nothing, but the process cost on the server side will be negligible, since our implementation will just ignore those unexpected properties, without allocating any memory for them.

Offline

#9 2014-08-26 12:13:19

hkcity1111
Member
Registered: 2014-08-18
Posts: 14

Re: Receive Json by Calculator Sample

Thank you for your enhancement. It is great!

Offline

#10 2015-04-15 08:29:12

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

Re: Receive Json by Calculator Sample

hello, AB

I reopen this old 3d because something escapes me ...

I Mormot the server with a method similar interface:

Download function (json: RAWutf8): TserviceCustomAnswer;

if I call (from Mormot client)

POST /BSWPZNOCNDTUG6DU63Z5V7LNFE/object.Download HTTP/1.1
Cache-Control: no-cache
Connection: Keep-Alive
Pragma: no-cache
Content-Type: application/json; charset=UTF-8
Accept: */*
Accept-Encoding: synlz
Authorization: Basic xxxxxx
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows; Synopse mORMot 1.18.1204 TWinHTTP)
Content-Length: 99
Host: test:888
Cookie: mORMot_session_signature=4DDC024C; mORMot_session_signature=4DDC024C

["{\"ID\":\"directory\",\"file\":\"filename\",\"seek\":0}"]


this works

but, reading this 3d
I try, from NON mormot client:

[{"ID":"directory","file":"filename","seek":0}]

but fails, also

{"ID":"directory","file":"filename","seek":0}

fails again

ps my model is sicPerSession

Offline

#11 2015-04-15 14:38:22

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

Re: Receive Json by Calculator Sample

You define a RawUTF8 input parameter, so the method expects a JSON string.

If you want the non mORMot client to send some true JSON array, use RawJSON as parameter type instead of RawUTF8.

Offline

#12 2015-04-15 16:35:20

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

Re: Receive Json by Calculator Sample

Tnxs

but then there are disadvantages to the client mormot?

Offline

#13 2015-04-15 21:08:41

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

Re: Receive Json by Calculator Sample

Not at all.

Offline

Board footer

Powered by FluxBB