简体   繁体   English

将xml张贴到Python3

[英]Curl xml post to Python3

I am trying to convert a Curl command into Python3. 我正在尝试将Curl命令转换为Python3。 But I am having issues inserting the header lines and sending the post request. 但是我在插入标题行并发送发布请求时遇到了问题。 The command is supposed to send a post request and print the formatted response. 该命令应该发送后请求并打印格式化的响应。

This is the command syntax: 这是命令语法:

curl -s 'https://api.sandbox.ebay.com/ws/api.dll'\
-H 'X-EBAY-API-SITEID: 0'\
-H 'X-EBAY-API-COMPATIBILITY-LEVEL: 861'\
--data '<?xml version="1.0" encoding="utf-8"?>
<GetCategoriesRequest xmlns="urn:ebay:apis:eBLBaseComponents">
    <LevelLimit>1</LevelLimit>
</GetCategoriesRequest>' | xmllint --format -

This is the code I have in the python version, I am missing the data, I think I need to use request.post but am really having a bad time translating the command. 这是我在python版本中拥有的代码,我丢失了数据,我想我需要使用request.post,但翻译命令的时间确实很长。

import requests
url = 'https://api.sandbox.ebay.com/ws/api.dll'
headers = { 'X-EBAY-API-SITEID': '0', 'X-EBAY-API-COMPATIBILITY-LEVEL': '861'}
r = requests.get(url, headers=headers)

You can send a post request using this code. 您可以使用此代码发送发帖请求。

import requests

headers = {
    'X-EBAY-API-SITEID': '0',
    'X-EBAY-API-COMPATIBILITY-LEVEL': '861'
}

data = '<?xml version="1.0" encoding="utf-8"?><GetCategoriesRequest xmlns="urn:ebay:apis:eBLBaseComponents"> <LevelLimit>1</LevelLimit></GetCategoriesRequest>'
url = 'https://api.sandbox.ebay.com/ws/api.dll'
r = requests.post(url, headers=headers, data=data)
print r.content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM