我正在努力开发发票API。 因此,我正在使用以下Python脚本来创建发票,但是它似乎是错误的:
import urllib.parse
import requests
import json
data = { "customer_id": 982000000567001, "contact_persons": [ "982000000870911", "982000000870915" ], "invoice_number": "INV-00003", "reference_number": " ", "place_of_supply": "TN", "gst_treatment": "business_gst", "gst_no": "22AAAAA0000A1Z5", "template_id": 982000000000143, "date": "2013-11-17", "payment_terms": 15, "payment_terms_label": "Net 15", "due_date": "2013-12-03", "discount": 0, "is_discount_before_tax": "true", "discount_type": "item_level", "is_inclusive_tax": "false", "exchange_rate": 1, "recurring_invoice_id": " ", "invoiced_estimate_id": " ", "salesperson_name": " ", "custom_fields": [ { "label": "Record Number", "value": 23 } ], "project_id": " ", "line_items": [ { "item_id": 982000000030049, "project_id": " ", "time_entry_ids": [ {} ], "expense_id": " ", "name": "Hard Drive", "product_type": "goods", "hsn_or_sac": 80540, "item_order": 1, "rate": 120, "quantity": 1, "unit": " ", "discount": 0, "tax_id": 982000000557028, "tax_exemption_id": 11149000000061054, "tax_name": "VAT", "tax_type": "tax", "tax_percentage": 12.5, "item_total": 120 } ], "payment_options": { "payment_gateways": [ { "configured": "true", "additional_field1": "standard", "gateway_name": "paypal" } ] }, "allow_partial_payments": "true", "custom_body": " ", "custom_subject": " ", "notes": "Looking forward for your business.", "terms": "Terms & Conditions apply", "shipping_charge": 0, "adjustment": 0, "adjustment_description": " ", "reason": " ", "tax_authority_id": 11149000000061052, "tax_exemption_id": 11149000000061054 }
headers = {"content-type": "application/x-www-form-urlencoded;charset=UTF-8","Authorization":"Zoho-authtoken <MY_TOKEN>","X-com-zoho-invoice-organizationid": "<MY_ORGANIZATION_ID>"}
url = "https://invoice.zoho.com/api/v3/invoices"
r = requests.post(url, data=json.dumps(data), headers=headers)
print(r.json())
我得到的是:{“代码”:1038,“消息”:“ JSON格式不正确»}
您能帮我告诉我如何编码我的数据吗?
答案 0 :(得分:0)
正如克劳斯(Klaus)在您的问题上指出的那样,此处的问题可能是您设置的“ ContentType”标头。我在Zoho的订阅API中遇到了类似的问题,但是可以通过以下方法解决它:
import requests
import json
from pprint import pprint
# This Script Will Need To Be Updated to new OAUTH SOON
ZOHO_AUTHTOKEN = 'PUTYOURAUTHTOKENHERE'
DUMMY_ORGANIZATION_ID = 'OrganizationIDFROMZOHO'
BASE_URL = "https://subscriptions.zoho.com/api/v1"
headers = {"Authorization": "Zoho-authtoken " + ZOHO_AUTHTOKEN}
subscription_id = '1234'
dict_data = {} #Put required data to send here
json_string = json.dumps(dict_data)
headers['ContentType'] = 'application/json'
updated_subscription_request = requests.put(BASE_URL+"/subscriptions/"+subscription_id, headers=headers, params={
"organization_id": DUMMY_ORGANIZATION_ID}, data=json_string)
pprint(updated_subscription_request.json())