如何解决Python HTTP POST请求被发送两次
我的代码有问题,我不知道我还能做些什么来解决这个问题,或者找到原因。
我已经开始更改Tumblr Python API Pytumblr(https://github.com/tumblr/pytumblr)以支持Tumblr(https://www.tumblr.com/docs/npf)的Neue Post Format(NPF)。 因此,我想将照片草稿发布到我的Tumblr帐户中。总而言之,它会执行应有的操作..但是它会发布两次。它在我的草稿中两次创建了相同的帖子,我不知道为什么。
从这里开始:
tumblr_client.create_photo("my blog name",state="draft",tags=["my","tags"],format="markdown",content=["URL to my photo"])
在另一个模块中,我的内容被创建为JSON对象(content_json),如下所示:
{
"content": [
{
"type": "image","media": [
{
"url": "my url"
}
]
}
],"state": "draft","tags": "my,tags","format": "markdown"}
现在我们进入请求模块。
我的OAUTH和HTTP请求标头配置:
def __init__(self,consumer_key,consumer_secret="",oauth_token="",oauth_secret="",host="https://api.tumblr.com"):
self.host = host
self.oauth = OAuth1(
consumer_key,client_secret=consumer_secret,resource_owner_key=oauth_token,resource_owner_secret=oauth_secret,signature_type='auth_header'
)
self.consumer_key = consumer_key
self.headers = {
"User-Agent": "pytumblr/" + self.__version,"Content-Type": "application/json"
}
这是POST功能。
def post(self,content_json,url,params={},files=[]):
"""
Issues a POST request against the API,allows for multipart data uploads
:param url: a string,the url you are requesting
:param params: a dict,the key-value of all the parameters needed
in the request
:param files: a list,the list of tuples of files
:returns: a dict parsed of the JSON response
"""
url = self.host + url
try:
if files:
return self.post_multipart(content_json,params,files)
else:
#data = urllib.parse.urlencode(params)
#if not PY3:
# data = str(data)
resp = requests.post(url,data=content_json,headers=self.headers,auth=self.oauth)
return self.json_parse(resp)
except HTTPError as e:
return self.json_parse(e.response)
程序一进入
resp = requests.post(url,auth=self.oauth)
它有两个草稿。
如果我通过邮递员发送我的内容,它只会发送一封帖子-应当如此。
谢谢:)
解决方法
可以自己解决! :)
我实现了此处所述的请求会话处理,从而达到了目的。 np.where
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。