Enable Dark Mode!
how-to-configure-json-rpc-and-xml-rpc-using-postman-for-sending-api-requests.jpg
By: Anagha C

How to Configure JSON RPC & XMLRPC using Postman for Sending API Requests

Technical Odoo 16

Postman is one of the best tools for Application Program Interface (API) testing. We can make any kind of API call, such as REST, SOAP, etc., using Postman. The Postman application is free to download, and it is very user-friendly. API methods like GET, PUT, DELETE, and POST are used. We can create/edit records using the get method, and we can send data through the API using the POST method. The PUT/PATCH method is used to update the existing data. DELETE is used for removing data from existing records.

We can easily install and configure Postman by referring our following blog: How to Configure Postman for Sending API Requests

This blog shows you how to create/edit records through the API by using JSON data from Postman.

Odoo provides two types of out-of-the-box APIs, which are the XMLRPC API and the JSONRPC API.

The JSON-RPC API is compatible with various other computer languages too, like Java, JavaScript, PHP, and others. Odoo JSONRPC API can be used with Python 3, as seen in this example.

Create a Python file inside the controller:

Using XMLRPC for sending API requests, Take the details of the product (API request) from the product.template and type=http

import json
from odoo import http
from odoo.http import request, Response
class TestApi(http.Controller):
@http.route('/Test/RestApi/fetch', type='http', method=['POST'], auth='public', csrf=False)
def fetch_product(self, **kwargs):
if request.httprequest.method == 'POST':
product = request.env['product.template'].sudo().search([('id', '=', kwargs['Id'])])
if product:
products = {
"productName": product.name,
"productCode": product.default_code,
"category": product.categ_id.name,
"uom": product.uom_id.name,
"price": product.list_price,
"cost": product.standard_price
}
return Response(json.dumps({
"status": "success",
"products": products,
}), headers={'content-type': 'application/json'})

You can specify the details of your request in the following tab.

how -to-configure-json-rpc-and-xml-rpc-using-postman-for-sending-api-requests-1-cybrosys

If you want to run a request, you need to specify the method, URL, and other parameters, such as auth. Set the endpoint URL to the Postman Echo sample API endpoint and specify the method; by clicking the Send button, you can send the request. Each URL you set in Postman represents an API endpoint. For each endpoint in an API, we know the specific URL, and this is what we set up in Postman. In the case of a third-party API, they will provide the URL, for example, which will be available in their API documentation itself. Or, assuming you're using your custom API, the URL will be the location and path you provided.

Use the tab params in Postman to send the parameters. All the Query parameters are added or appended to the end of the URL following the ‘?’ and key-value pairs which are separated by the ‘&’. We can add request parameters in the Param's Tab or directly add their URL, where we can enter the ID of the parameter and its associated value.

We can run this API through Postman, and product details will be generated. We can fetch the product corresponding to that ID by this XML-RPC using Postman.

how -to-configure-json-rpc-and-xml-rpc-using-postman-for-sending-api-requests-2-cybrosys

Setup type= ‘json’ and fetch data according to the data given in the JSON body of the API request.

Here, changing the product name through this API by using the parameters product name as newName and id from the json body.

from odoo import http
from odoo.http import request
class TsetApi(http.Controller):
    @http.route('/Test/RestApi/Product', type='json', auth='public',
                method=['POST'], csrf=False)
    def test_api(self):
        if request.httprequest.method == 'POST':
            for rec in request.jsonrequest['products']:
                product = request.env['product.template'].sudo().search([('id', '=', rec.get('itemNumber'))])
                product_update = product.update({
                    'name': rec.get('newName'),
                })
                return {
                    'productName': product.name
                }

how -to-configure-json-rpc-and-xml-rpc-using-postman-for-sending-api-requests-3-cybrosys

Here, we can see the updated name in response and data for updating in the JSON body. When we pass the request with an updated name in the JSON body, it will affect the corresponding product in the backend based on the ID passed here.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message