首页 > 解决方案 > 如何修复 requests.put 方法,因为它显示状态错误代码 500?

问题描述

我正在尝试将 http 请求从客户端发送到要更新页面的服务器。我尝试使用虚拟 html 代码测试我的 requests.put 方法是否有效,并且确实有效。这是我使用的有效代码。

imports requests
from requests.auth import HTTPBasicAuth

headers = {
    'Content-Type': 'application/json',
}
pass_string = "<p>This is the updated text for the new page. Created Automatically through script. Experiment successful.</p>"
data = '{"id":"525424594","type":"page", "title":"Update Status","space":{"key":"CSSFW"},"body":{"storage":{"value":"' + pass_string + '","representation":"storage"}}, "version":{"number":17}}'
response = requests.put('https://confluence.ai.com/rest/api/content/525424594', headers=headers, data=data, auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))

但是,一旦我使用要为我的任务传递的 html 字符串,我就会得到一个错误状态代码 500,说明“内部服务器错误”。这是我的代码:

imports requests
from requests.auth import HTTPBasicAuth
from bs4 import BeautifulSoup

pass_string = "<p><br /></p><table><colgroup><col /><col /><col /><col /><col /><col /></colgroup><tbody><tr><th><p>JIRA</p></th><th><p>Type</p></th><th><p>PR</p></th><th><p>Commit</p></th><th><p>Author</p></th><th><p>Date</p></th></tr><tr><td><p>IAP-5742</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td></tr><tr><td><p>IAP-5991</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td></tr><tr><td><p>IAP-5971</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><p><br /></p></td></tr><tr><td><p>IAP-6200</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><p><br /></p></td></tr><tr><td><p>IAP-5608</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><br /></td></tr><tr><td><p>IAP-5075</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><p><br /></p></td></tr><tr><td><p>IAP-5757</p></td><td><p>Bug</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><br /></td></tr></tbody></table><p class=\"auto-cursor-target\"><br /></p>"

headers = {
    'Content-Type': 'application/json',
}

data = '{"id":"525424594","type":"page", "title":"Update Status","space":{"key":"CSSFW"},"body":{"storage":{"value":"' + pass_string + '","representation":"storage"}}, "version":{"number":17}}'
response = requests.put('https://confluence.ai.com/rest/api/content/525424594', headers=headers, data=data, auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))

请注意,pass_string 已直接从网站的 rest/api/content 输出中复制,即从https://confluence.ai.com/rest/api/content/525424594?expand=body.storage复制,因此应该字符串没有任何问题,但它仍然显示错误代码 500,我无法理解它为什么会这样做。

标签: pythonjsonhtml-tablepython-requestsconfluence-rest-api

解决方案


推荐阅读