REST Python Implementation

Hi, I’m trying to implement the REST API in my application written in Python3.
I’m very new to this kind of REST stuff so please forgive my lack of experience.

My first question is about the enpoint:

The web services are accessible ws endpoint, for example:
http://localhost:8080/open-platform-demo/ws/

I run Axelor in a windows docker and the end point seems to be :
http://localhost:8080/ws/
is it correct ? or must I use another endpoint ? wich one in my case ?

Here is the python code I tried:
import requests

auth_data = {
  "username": "admin",
  "password" : "admin"
}

new_contact = {
  "data": {
    "firstName": "John",
    "lastName": "Smith",
    "email": "j.smith@gmail.com",
    }
}

# Connect and authenticate
resp = requests.post('http://localhost:8080/ws/login.jsp', )
if resp.status_code != 200:
    print(f'ERROR POST /login.jsp/ status code = {resp.status_code}')
else :
    print (f'SUCCESS POST /login.jsp/ status code = {resp.status_code}')


# Read a record
resp = requests.get('http://localhost:8080/ws/rest/com.axelor.contact.db.Contact')
if resp.status_code != 200:
    print(f'ERROR GET /ws/rest/com.axelor.contact.db.Contact/ status code = {resp.status_code}')
else:
    print(f'SUCCESS GET /ws/rest/com.axelor.contact.db.Contact/ status code = {resp.status_code}')

# Create a new record
resp = requests.post('http://localhost:8080//ws/rest/com.axelor.contact.db.Contact', json=new_contact)
print (f'status code = {resp.status_code}')
if resp.status_code != 201:
    print(f'ERROR GET /ws/rest/com.axelor.contact.db.Contact/ status code = {resp.status_code}')
else :
    print(f'Created contact. firstName: {resp.json()["firstName"]}')

This give me the following results:

SUCCESS POST /login.jsp/ status code = 200
SUCCESS GET /ws/rest/com.axelor.contact.db.Contact/ status code = 200
status code = 401
ERROR GET /ws/rest/com.axelor.contact.db.Contact/ status code = 401

I think I miss something basic.

Thanks in advance,
Best regards,
Rafael

we should search the forum too, here is linked post, let me know if you still need more information, we did with php laravel

Thanks for your reply
I did search and saw this post which say the documentation is not up to date.
But this post only deals with Authentication, I need some more explaination about the Axelor’s RESP API usage in Python3.
How I can know the exact URL I have to use to get the right endpoint ?

see if it can help you, let me know if you need more input on this

https://docs.axelor.com/adk/5.2/dev-guide/web-services/index.html

Sorry but it does not help. I do understand this guide but I still not know the exact URL I have to use.
I also need more advice about the authentication procedure.
Please provide a working exemple with GET and POST (in any language)

Thanks

is it correct ?

@RAF make sure you use session client so that the subsequent requests after login get proper session cookie.

Does anyone have any news about this ?

rest is fine, we have used it

I use it with .net , no problem

Hi there,
we still have the problem that we cannot connect to the database. We only get error 401 (auth error)

Can you provide an example script so we can test it? We tried multiple variations but nothing works. We use Python for this task.

Many thanks in advance!!!
PSa

share the gist might be we can help

Hey,

with regard to the post of PSa. The main goal is, being able to access all the products of manufacturing for instance. But first, authentications are not working. We´ve tried the following:

First:

import requests

auth_data = {

"username": "admin",

"password" : "admin"

}

# Connect and authenticate

resp = requests.post("http://localhost:8080/ws/rest/login.jsp", auth_data)

print(resp)

Yet, it doesn’t matter, whether I change the auth_data, I´ll always get the response 200. Which can not be right. Moreover, the Axelor Enterprise Application does not react.

Then, I used Postman/Insomnia for creating a request:

import requests

url = "http://localhost:8080/ws/rest/login.jsp"

payload = {

"username": "admin",

"password": "admin"

}

headers = {

"cookie": "CSRF-TOKEN=5ff6776f-4201-4bcb-b42d-d39c31d5bbd4; JSESSIONID=A0DC6FFF9D5516A8EEFC28F68854EDC2",

"X-Requested-With": "XMLHttpRequest",

"Content-Type": "application/json"

}

response = requests.request("GET", url, json=payload, headers=headers)

print(response.text)

In the console I get the response:

<!doctype html>HTTP Status 401 – Unautorisiertbody {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}

HTTP Status 401 – Unautorisiert


Type Status Report

Beschreibung The request has not been applied because it lacks valid authentication credentials for the target resource.


Apache Tomcat/8.5.69

In the Axelor Enterprise Application:

INFO c.a.a.p.AuthPac4jModuleLocal$AxelorFormClient - AJAX request detected -> returning the appropriate action

Can you help us with that?

Thank you!
PWa

Hi all,

I’m surprised I’m not the only one with this problem, and Axelor doesn’t take a minute to respond, give a working example, or fix the problem as it seems like a real problem.
As Axelor is a French company and I am French too, I will phone them this afternoon to ask the team directly.

2 J'aimes

Hi,
have you already received an answer?

Hi RAF,

Here’s a python script that retrieves all products using REST services:

import requests

def main():
	url = "http://localhost:8080/Gradle___com_axelor___axelor_erp_6_1_0_war__exploded_/"
	rest_endpoint = "ws/rest/"

	with requests.session() as session:
		auth_payload = {'username': 'admin', 'password': 'admin'}
		auth_response = session.post(url + 'login.jsp', data=auth_payload)

		product_response = session.get(url + rest_endpoint + "com.axelor.apps.base.db.Product")
		print(product_response.json())

if __name__ == "__main__":
	main()

If you want the script to run properly on your local, you’ll have to change the values of the following variables:

  • url
  • auth_payload

If the authentication fails, the following message will appear in ther server log:
.p.AuthPac4jModuleLocal$AxelorFormClient : Password authentication failed for user: <username>

and the line 15 of the script will throw an exception.

The script was tested with Python 3.8.10 and requests 2.22.0.

Best Regards