Connect to Tryton with JSON-RPC and PHP

Hello,
I use tryton in the company and I want to give some customers the possibility to insert some movements.
The Tryton web client is a troopo articulated for the people who will have to make the insertions.
For this reason I would like to implement a project in Laravel (PHP), which interfaces to the tryton database through RPC, which has a very friendly interface.

I can not find documentation on how to use RPC.

The operations I should perform are:

  • User Login and User Logout
  • Various Queries (products, parties and moves)
  • Insert in Tryton of Internal Shipment and Return Customer Shimpment.

Can you give me suggestions and examples?

Thanks so much

1 Like

We have build a webapp based on EmberJS (Javascript) to lend products to users. The app is directly talking to Tryton (like sao, the webapp). We have also some Python scripts which can directly talk to Tryton.

The main thing is that you have to do JSON-RPC calls via HTTP. When you want to login send a request something like {‘method’: ‘common.db.login’, ‘params’: [‘admin’, {‘password’ : ‘your password’}] } . Tryton will return some data like session id which you should use in the http-header for the next calls (run Tryton server in debug mode in the foreground so you can see messages). Then call to get the user preferences (also needed). After that you can do anything in the database as long as you stay with the right syntax.

1 Like

I’m running tests on a local server. I am sending JSON requests like:
{ ‘method’:‘common.server.version’, ‘params’:[null, null], ‘id’: 1 }

The trytond server responds with the message: HTTP 1.1 - 500.
Why do I always receive “Internal Server Error”?

Can someone post me a correct syntax of sending JSON-RPC request?
Can this request be sent in Jscript (Jquery)?

Thanks

We need to have the full call: URL, headers etc.
But I guess you are not sending to the right URL.

With:
{
url : “http://127.0.0.1:8000/”,
type: “POST”,
contentType: “application/json”,
data: {“jsonrpc”: “2.0”,“method”: “common.server.version”, “params”:[null,null],“id”:1},
dataType: “json”
}
The trytond server responds with the message: “OPTIONS / HTTP/1.1” 405 -

Tryton is based on jsonrpc 1.0 but it should not be an issue to use 2.0.

It looks like an OPTIONS query is sent and not a POST.

I can not get a positive response from the server

With the following code:

            $.post(
                'http://localhost:8000/',
                JSON.stringify({ 'method':'common.server.version', 'id':1, 'params': [null, null] }),
                'json'
            ),
            $.ajaxSetup({
                headers: {
                    'X-Requested-With': 'XMLHttpRequest',
                    'Accept': 'application/json-rpc; charset=utf-8',
                    'Content-Type': 'application/json',
                },
            });

the server recognizes the post method, but I get error 500:

907 123145399373824 [2018-10-01 09:48:16,968] INFO werkzeug 127.0.0.1 - - [01/Oct/2018 09:48:16] "POST / HTTP/1.1" 500 -

If you are working in the browser, be sure to run also your javascript from exactly the same address:port as you run Tryton from. Hence you need Tryton to serve your index.html and javascript files. You will run in trouble with CORS (cross site scripting) which is hard disabled in the newest browsers. See also your development console in the browser, vital information is showed there. I’m also using console.log() a lot.

An extreem basic simple Python 2 script which will work with Tryton 4.6:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from jsonrpclib import Server as ServerProxy

server = ServerProxy('http://<ip-address>:<port>', verbose=1)
result = server.common.server.version([])
print result

You will get an error that the response is not a Dict, but you can see that it comes back with the server version.

The error is that you call $.ajaxSetup inside the $.post call. So ajax is setup too late and so it works for the next call.

Hi,
I moved the $ .ajaxSetup call before the $ .post call but the problem is the same 

I can not understand where I’m wrong 


Could someone send me a working script (in jQuery or Php) to understand the correct sending of the RPC call to the Tryton server?

Thanks so much

Here is a simple query to the demo server (it must be run inside a page from the demo server):

$.ajax({'contentType': 'application/json', 'data': JSON.stringify({ 'method':'common.server.version', 'id':1, 'params': [null, null] }), 'dataType': 'json', 'url': 'http://demo5.0.tryton.org/', 'type': 'post'})

Script tested on local server (http: // localhost: 8001) on Safari browser and Chrome browser:

7311 123145474113536 [2018-10-05 09: 57: 31,188] INFO werkzeug 127.0.0.1 - - [05 / Oct / 2018 09:57:31] “OPTIONS / HTTP / 1.1” 405 -

without ‘contentType’: ‘application / json’:

7311 123145474113536 [2018-10-05 09: 57: 19,283] INFO werkzeug 127.0.0.1 - - [05 / Oct / 2018 09:57:19] “POST / HTTP / 1.1” 500 -


I’m using MAC OSX 10.14 with Python 2.7.1
Tryton 4.6 in viertualenv environment
Terminal: (env)> trytond -c trytond.conf -v
File test.html launched on safari and chrome browser

The example does not request to make an OPTIONS call. I guess if it does it is to retrieve CORS because of cross-domain. This means that you are not running the command from a page from the same domain.

Server and RPC call take place in the same machine.

I’ll try on a linux server, installing Tryton without virtualenv

You will need to support CORS (Tryton does not have such option in base, it should be managed by an external tool) or use a reverse proxy that dispatch to the right server.

I was able to execute the request correctly: laravel uses a different virtual server than the Tryton server. I created a page in html and called by Tryton’s localhost. Now works!
I tried to use the common.db.login call: from the server log I read that the login was executed, but how do I recover user and session? I tried querying an array created for this purpose with array [0] and array [1], but I get nothing. I tried to get the user ID with array [‘id’], but the session can not get it. can you give me a hand?

If I want to insert an internal shipment how should I dial the call? and to have all the customer shipment?

thank you

Hello to all,
I managed to get call id, user id and session.
I’m learning Tryton’s json-rpc calls and I still have some gaps 

After logging in (using the common.db.login method), is there a special procedure to save session data?
I’ve noticed that every time you run common.db.login the session changes.
I read that you need to insert user id and session at the first and second place in params.
However, I do not know the correct syntax for adding a new internal shipment and to be able to view the customer shipment list of a specific party ordered by actual date.

Who gives me help?

Thanks

You should use the create method to create new records and the search method to find existing ones. If you want to perform a search and return a fixed amount of fields using search_read will reduce the number of requests to perform

I need an example: I do not know how to dial the json call to add / edit.
What should I write in the “method” field?
What should I write in the “params” field?

Unfortunately, on the net there are no examples of real use of Tryton’s json-rpc

Thanks

My test:

{'contentType': 'application/json',
'data': JSON.stringify({'id':3, 'method':'model.res.user.get_preferences', 'params':[1, dd69aedbd0a6b0caba47bba9f1e3e7f8d8a258d1fa3b6e66f0697797981fc546, true, {}] }),
'dataType': 'json',
'url': 'http://localhost:8001/tryton/',
'type': 'post'}

The server responded with a status of 401 (UNAUTHORIZED)