我在python中使用request
包进行了几次Web服务调用,其中一个是纯粹的表单和WORKS:
r = requests.post('http://localhost:5000/coordinator/finished-crawl', \
data = {'competitorId':value})
另一个使用JSON但不起作用:
service_url = 'http://localhost:5000/coordinator/save-page'
data = {'Url': url, 'CompetitorId': competitorID, \
'Fetched': self.generateTimestamp(), 'Html': html}
headers = {'Content-type': 'application/json'}
r = requests.post(service_url, data=json.dumps(data), headers=headers)
现在如果不包含标题我使用上面的标题,我得到404,但如果我不包括
r = requests.post(service_url, data=json.dumps(data))
我得到了415.我已经尝试查看stackoverflow上的其他帖子,并且从我可以告诉的电话是正确的。我通过应用程序邮递员测试了Web服务,它可以工作。有些人可以告诉我什么是错的或指出我正确的方向?
完整的方法
def saveContent(self, url, competitorID, html):
temp = self.cleanseHtml(html)
service_url = 'http://localhost:5000/coordinator/save-page'
data = {'Url': url, 'CompetitorId': competitorID, \
'Fetched': self.generateTimestamp(), \
'Html': temp}
headers = {'Content-type': 'application/json'}
r = requests.post(service_url, json=json.dumps(data), headers=headers)
并且cleanseHTML:
def cleanseHtml(self, html):
return html.replace("\\", "\\\\")\
.replace("\"", "\\\"")\
.replace("\n", "")\
.replace("\r", "")