Python 尝试将XML字符串发送到Carrot2服务器时发生ParseError

Python 尝试将XML字符串发送到Carrot2服务器时发生ParseError,python,xml,http,post,carrot2,Python,Xml,Http,Post,Carrot2,当我将此请求发送到我的Carrot2服务器时: http = httplib2.Http() my_url = 'http://localhost:8080/dcs/rest?dcs.c2stream=xml' xml_string = etree.tostring(xml) http.request(my_url, 'POST', body=xml_string, headers={'Content-type': 'application/x-www-form-urlencoded'}) 我

当我将此请求发送到我的Carrot2服务器时:

http = httplib2.Http()
my_url = 'http://localhost:8080/dcs/rest?dcs.c2stream=xml'
xml_string = etree.tostring(xml)
http.request(my_url, 'POST', body=xml_string, headers={'Content-type': 'application/x-www-form-urlencoded'})
我得到以下回应:

{
    'status': '400', 
    'content-length': '1571', 
    'server': 'Jetty(7.3.1.v20110307)', 
    'cache-control': 'must-revalidate,no-cache,no-store', 
    'access-control-allow-origin': '*', 
    'content-type': 'text/html;charset=ISO-8859-1'
}, 
<html>\n
<head>\n
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>\n
    <title>Error 400 Could not parse Carrot2 XML stream: ParseError at [row,col]:[1,1]\nMessage: Content is not allowed in prolog.</title>\n
</head>\n
<body><h2>HTTP ERROR 400</h2>\n
    <p>Problem accessing /dcs/rest. Reason:\n
        <pre>    Could not parse Carrot2 XML stream: ParseError at [row,col]:[1,1]\nMessage: Content is not allowed in prolog.</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>\n
</body>\n
</html>\n
{
“状态”:“400”,
“内容长度”:“1571”,
“服务器”:“Jetty(7.3.1.v20110307)”,
“缓存控制”:“必须重新验证,无缓存,无存储”,
“访问控制允许来源”:“*”,
“内容类型”:“text/html;charset=ISO-8859-1”
}, 
\n
\n
\n
错误400无法分析Carrot2 XML流:ParseError位于[row,col]:[1,1]\n消息:prolog中不允许包含内容。\n
\n
HTTP错误400\n
访问/dcs/rest时出现问题。原因:\n
xml = etree.Element('searchresult')
etree.SubElement(xml, 'query').text = 'dogs are fantastic pets'
for result in search_results:
    doc = etree.Element('document')
    etree.SubElement(doc, 'title').text = result['title'].replace('<b>', '').replace('</b>', '').replace('</b', '')
    etree.SubElement(doc, 'snippet').text = result['abstract'].replace('<b>', '').replace('</b>', '').replace('</b', '')
    etree.SubElement(doc, 'url').text = result['url'].replace('<b>', '').replace('</b>', '').replace('</b', '')
    xml.append(doc)
无法分析Carrot2 XML流:在[row,col]:[1,1]处发生ParseError]\n消息:prolog中不允许包含内容。


由Jetty提供动力://
\n \n \n
我做错什么了吗?我不确定要找什么来找到问题

编辑: 我使用以下代码构建了XML字符串

xml=etree.Element('searchresult')
SubElement(xml,'query').text='dogs是很棒的宠物'
对于搜索结果中的结果:
doc=etree.Element('文档')

etree.SubElement(doc,'title').text=result['title'].replace('','').replace('','').replace(“您发布的XML字符串是什么?”@stanislaw.osinski我已将用于构建XML字符串的代码添加到问题中。一个问题是您发送的内容类型不正确……我应该使用哪种内容类型?@JosiahWitt您看到的错误消息通常是XML字符串以非白色开头的结果空格或大括号。您可以发布您正在提交的实际XML字符串吗?