gpt4 book ai didi

python - 如何使用请求库发送 xml 正文?

转载 作者:IT老高 更新时间:2023-10-28 21:41:13 26 4
gpt4 key购买 nike

def request():
#encoded_xml = urllib.urlencode({'XML': read_xml()})
#encoded_xml = read_xml()
headers = {'Authorization': AUTH_TOKEN,\
'developerToken': DEVELOPER_TOKEN,\
'clientCostumerID': CLIENT_ID}
content = {'__rdxml': encoded_xml}
#content = encoded_xml
#content = {'__rdxml': read_xml2()}
r = requests.post(URL, data=content,\
headers=headers)
return r

这些组合似乎不起作用。

由于某种原因没有设置标题。

最佳答案

直接发送xml字节:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import requests

xml = """<?xml version='1.0' encoding='utf-8'?>
<a>б</a>"""
headers = {'Content-Type': 'application/xml'} # set what your server accepts
print requests.post('http://httpbin.org/post', data=xml, headers=headers).text

输出

{
"origin": "x.x.x.x",
"files": {},
"form": {},
"url": "http://httpbin.org/post",
"args": {},
"headers": {
"Content-Length": "48",
"Accept-Encoding": "identity, deflate, compress, gzip",
"Connection": "keep-alive",
"Accept": "*/*",
"User-Agent": "python-requests/0.13.9 CPython/2.7.3 Linux/3.2.0-30-generic",
"Host": "httpbin.org",
"Content-Type": "application/xml"
},
"json": null,
"data": "<?xml version='1.0' encoding='utf-8'?>\n<a>\u0431</a>"
}

关于python - 如何使用请求库发送 xml 正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12509888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com