微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python HTTP POST请求被发送两次

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?