AI Zone Admin Forum Add your forum

CX Company, chatbot market leader in the Netherlands since 2005, will speak on the Amsterdam Chatbots Meetup on Wednesday, Oct 18!

JSON to Firebase communication (part2)
 
 

I’m sorry to come back with this question again but ultimately it does not work.
I do not know why I thought it worked, maybe because my 2 year old son was running around and I was not concentrated enough.

therefore, I remove the problem of ports on my server since
curl -X POST -d ‘{ "usname" : "testUser6" }’  ‘https://myfirebase.firebaseio.com/users.json’
works correctly and returns:
{ "Name": "- Kw-y9sZtYlEn9IdJf08"}
what is OK

I may multiply the various tests,
anything that resembles

$_url https://myfirebase.firebaseio.com/users.json
$_jsonmyvar = ^jsoncreate (transient object)
$_jsonmyvar.usname testUser6

$_results 
= ^jsonopen (transient POST $_url $_jsonmyvar $$headers

does not work.

I call $_results with

t: Do you whant me to send test8?
        
a: (~ yes$_results OK it's done
        a: (~ no) Ok, no problem 

then I listen to Firebase’s response with

u: (<\[ name\]name is _0 

but nothing happens.

In the JSON documentation I do not understand exactly what ^ jsoncreate() returns exactly. Is this really ‘{"usname": "testUser6"}’ in my case?

 

 
  [ # 1 ]

In CS, JSON objects/arrays are stored as facts.
^jsoncreate() doesn’t do any more than return a unique string to be used as the subject for those facts. This unique string will be of the form “jo-nnnn” (for an object) or “ja-nnnn” (for an array). The nnnn is the next free number. (it is possibly to tweak that label a bit via ^jsonlabel() but you can ignore that for now).

For a JSON object, the property names are then the verbs and the value is the object.

So in your example, the ^jsoncreate() will return something like jo-t1 (t for transient), and the next line is equivalent to:

^createfactjo-t1 usname testUser6 FACTTRANSIENT

I don’t know what you mean by “listening to Firebase’s response”. The ^jsonopen() call will have made the request and automatically parses the response, i.e. creates facts, leaving you with a top level reference to the JSON. Nothing from the response body is held anywhere else.

But if you stored the reference in a global variable, then you could have a ^respond() could match something like:

u: ( $$results.Name Yes that is validthe value is $$results.Name 

 

 

 
  [ # 2 ]

Oh and before Bruce corrects me, that equivalent ^createfact() for a JSON object should include a few other JSON specific flags.

 

 
  [ # 3 ]

Thank you for your answer Handy, it is always very clear when you explain things.
The JSON documentation is also well done on this subject, but what worries me is the signs like ’ and ‘’ to which my BaaS seems very sensitive ... and me too (I am French speaking and sometimes a little lost in translation ).

So if

$_jsonmyvar = ^jsoncreate (transient object)
$_jsonmyvar.usname = testUser6 


do

^createfact( jo-t1 usname testUser6 FACTTRANSIENT)  

my output will replace $_jsonmyvar by :

{ ''usname'' : ''testUser6'' }

then how to be sure to get the expected presentation surrounded by simple quotes ?

‘{ ''usname'' : ''testUser6'' }’

Is it that’s ^jsonopen do or did I put something like

^jsonopen (transient POST $_url \'$_jsonmyvar\' $$headers)  

?

 

 
  [ # 4 ]

You shouldn’t have to worry about the single quotes.

In your curl command line you had:
curl -X POST -d ‘{ "usname" : "testUser6" }’  ‘https://myfirebase.firebaseio.com/users.json’

The single quotes here are merely delimiters for the -d parameter so that curl knows where to start and stop. The backend service is not going to expect single quotes around a JSON payload. That is not valid JSON.

But I’ve just noticed that you are not setting a content type in the headers, so perhaps your service is defaulting to some text format and then tries to be smart, stripping enclosing quotes and then auto-detecting JSON. Which is clever, if not confusing.

So I would recommend setting a Content-Type header to application/json.


But, if you want to continue trying to wrap the data in quotes, then you should do:

$_data = ^jsonwrite($_jsonmyvar)
$_data = ^join(\' $_data \')
$_results = ^jsonopen(transient POST $_url $_data $$headers

If you pass a JSON variable to ^jsonopen() then the engine will automatically perform the serialization, this is handy when the JSON is very large - you don’t really want to be passing large strings around.
In the above alternative you are doing the serialization and then adding to the string. ^jsonopen() just sees a string and just uses it as is.

 

 

 
  [ # 5 ]

I have to be quite silly. I take the problem in all directions but nothing works.
Is there anyone who can communicate with Firebase?

Actually playing with the quotes’ does not change the matter ... but what is more sad for me is that informing the headers does not make me any more successful.

here is my last test failed :

$_url https://myfirebase.firebaseio.com/users.json
$_jsonmyvar = ^jsoncreatetransient object )
$_jsonmyvar.usname testUser12

$_userAgent 
= ^"User-Agent: %bot, ChatScript"
$_header = ^"~Accept: application/json 
                  
$_userAgent
                  ~Content-Type: application/json"
                  
$_response = ^jsonopen(transient POST $_url $_jsonmyvar $_header $_userAgent

t: do you whant me to send test 12 info ?
        
a:(yes$_response
        b
:(nook no problems 

for test purposes I use a ChatScript server hosted on Ubuntu Amazon EC2. I connect via the simple php interface to: build and unfold my script.

I’m blocked, so open to any suggestion ...

 

 
  [ # 6 ]

First thing I would do is do
:trace json

so I could get a log of what was sent and received via json open. Post that here and maybe we will spot something

 

 
  [ # 7 ]

:trace json
Enabled simple: echo
Enabled deep detail: json
Disabled simple: match ruleflow variables
Disabled mild detail: output pattern prepare
Disabled deep detail: always fact hierarchy infer label macro pos query sample sql substitute tcp topic user userfact usercache varassign
trace = 553648129 (0x21000001)

 

 
  [ # 8 ]

I actually wanted you to do that and THEN run your code. the user log file would then show the call and you could share that.

 

 
  [ # 9 ]

One other thing you can do is to check %httpresponse directly after the ^jsonopen() call. It should be 200 if all successful.
Also add

if ($_response) ^log(Firebase responded with ^jsontree($_response)) 

There is absolutely nothing wrong with your code leading up to the ^jsonopen() call, so lets check the results.

 

 
  [ # 10 ]

Everything works fine now I had some errors in my code that the debug commands allowed me to fix. I’m sorry to have bothered you with this trivial problem.

Thank you Andy and tank you Bruce for your help, and thank you all for the documentation.

 

 
  login or register to react