【问题标题】:getting 415 error while posting in requests in rest api , trying to run cms query在 rest api 中发布请求时出现 415 错误,尝试运行 cms 查询
【发布时间】:2021-05-25 21:42:11
【问题描述】:

您好,我是 python 和 REST API 的新手,

我在尝试使用 requests.post 在 cms 中运行查询时遇到 415 错误

我无法将content-typeAccept 与登录令牌一起传递。 我可以在 talend 中与这 2 个标头一起运行它。

你能帮我看看如何在 requests.post 最后添加这两个标题吗?

下面是我的代码

import requests
from lxml import etree
import xml.etree.ElementTree as ET
import pandas as pd
import openpyxl as x
from bs4 import BeautifulSoup
import xmltodict
protocol='http'
host='HOST'
port='6405'
content_type='application/xml'
base_url = protocol + '://' + host + ':' + port
bip_url = base_url + '/biprws'
webi_url = bip_url + '/raylight/v1'
sl_url = bip_url + '/sl/v1'
headers_auth = {
        'Content-Type' : content_type,'Accept':'application/xml'
        }
headers = {
    }      
username = 'user'
password = 'pass'
auth = requests.get(bip_url + '/logon/long', headers=headers)
root = etree.fromstring(auth.text)
root[3].text = username
root[0].text = password 
etree.tostring(root)
send = requests.post(bip_url + '/logon/long',
        headers=headers_auth,
        data=etree.tostring(root))
tokenresp = etree.fromstring(send.content)
headers['X-SAP-LogonToken'] = tokenresp[3][0][0].text
folder_get = requests.get(bip_url + '/v1/cmsquery', headers=headers)
folder_root = etree.fromstring(folder_get.text)
Query_var = 'SELECT SI_ID,SI_NAME FROM CI_INFOOBJECTS WHERE SI_ANCESTOR = 12141'
folder_root[0].text = Query_var
data1 = etree.tostring(folder_root)
folder_post = requests.post(bip_url + '/v1/cmsquery', headers = headers, data = data1)
folder_post.status_code

【问题讨论】:

    标签: python-3.x restful-authentication business-objects


    【解决方案1】:

    我认为 415 表示您正在传递 API 不接受的内容类型。您需要正确配置标题。试试这个:

    headers = {
        'Content-Type' : 'application/xml'
    }
    auth = requests.get(bip_url + 'logon/long', headers=headers)
    print(auth.status_code)
    

    看起来您的问题是您将标题设置为空白字典。

    【讨论】:

    • 非常感谢,它符合您的建议。再次感谢
    • @ManishKothari 你可以接受这个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多