I have an API of one company to where I have to post my data from odoo. I want to use the requests module for this. But I don't know if it is applicable or not in Odoo. There are some data I have to post like username, password, invoice number, total sales, etc.
How can I send data to an external API? The data has to be sent in JSON format.
And what is the other way of sending data from odoo to other API?
I have a button in invoice form when that button is clicked the data has to be posted and get response message. I have tried this.
# -*- coding: utf-8 -*-
import datetime
import requests
import logging
from odoo import models, fields, api
logging.basicConfig(format='%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y %H:%M:%S',
level=logging.DEBUG,
filename='logs.txt'
)
logger = logging.getLogger('ird')
class invoice_ird(models.Model):
_inherit = "account.invoice"
def send_bill_ird(self):
payload = {
'username': "Test_CBMS",
'password': "test@321",
'seller_pan': f"{self.company_id.vat}",
'buyer_pan': f"{self.partner_id.vat}",
'buyer_name': f"{self.partner_id.parent_id}",
'fiscal_year': "2076.077",
'invoice_number': f"{self.number}",
'invoice_date': f"{self.date_invoice}",
'total_sales': self.amount_total,
'taxable_sales_vat': self.amount_untaxed,
'vat': self.amount_tax,
'excisable_amount': 0,
'excise': 0,
'taxable_sales_hst': 0,
'hst': 0,
'amount_for_esf': 0,
'esf': 0,
'export_sales': 0,
'tax_exempted_sales': 0,
'isrealtime': True,
'datetimeclient': datetime.datetime.now(),
}
r = requests.post('http://103.1.92.174:9050/api/bill', json=payload)
logger.info(f'Response Messages: {r}, {r.status_code}')
#print(r)
#print(r.text)
Why doesn't my logs.txt file is created and how can I see my response message? Is there any best way to work?
My XML file
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="invoice_to_ird" model="ir.ui.view">
<field name="name">inovice.to.ird</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<xpath position="after" expr="//form[@class = 'o_invoice_form']/header/button[@name = 'action_invoice_draft']">
<button class="oe_highlight" name="send_bill_ird" type="action" string="Upload To IRD" attrs="{'invisible':[('state', '!=','paid')]}" />
</xpath>
</field>
</record>
</odoo>
you trying to send them dictionary instead of json data. To fix this change
r = requests.post('http://103.1.92.174:9050/api/bill', json=payload)
to
r = requests.post('http://103.1.92.174:9050/api/bill', data=json.dumps(payload))