MiraiForum

    • Register
    • Login
    • Search
    • Popular
    • Recent
    • Unsolved
    • Tags
    • Groups
    • 友情链接

    Solved 求问如何使用Python的requests库mirai-api-http中上传图片

    使用交流
    3
    4
    49
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Michael1015198808 last edited by

      查阅文档之后看到需要以 multipart/form-data 格式上传,然后查了一下怎么用requests上传这种格式,写出了如下代码

      with open("/tmp/c_exam.png", "rb") as f:
          res = requests.post(
              "http://localhost:9999/uploadImage",
              headers={
                  "sessionKey":session_key,
                  "type":"friend",
              },
              files = {
                  "img": ("/tmp/c_exam.png", f.read())
                  # "img": ("/tmp/c_exam.png", b"")
              }
          )
      

      然而res.text内容为{"code":400,"msg":"无效参数"}
      实在想不出如何解决,用Google搜索mirai-api-http + requests + 图片也没找到合适的解法orz

      1 Reply Last reply Reply Quote 0
      • M
        Michael1015198808 last edited by Michael1015198808

        file_path = "/tmp/c_exam.png"
        with open(file_path, "rb") as f:
            imageUrl = json.loads(requests.post(
                "http://localhost:9999/uploadImage",
                files = {
                    "sessionKey":(None, session_key),
                    "type":(None, "friend"),
                    "img": (file_path, f.read())
                }
            ).text)["url"]
        

        解决了解决了orz

        1 Reply Last reply Reply Quote 0
        • cssxsh
          cssxsh last edited by

          你可以看看这个
          https://toolbelt.readthedocs.io/en/latest/uploading-data.html

          1 Reply Last reply Reply Quote 0
          • D
            diyigemt last edited by

            如果写python可以试试nonebot2+mirai adapter的形式

            1 Reply Last reply Reply Quote 0
            • M
              Michael1015198808 last edited by Michael1015198808

              file_path = "/tmp/c_exam.png"
              with open(file_path, "rb") as f:
                  imageUrl = json.loads(requests.post(
                      "http://localhost:9999/uploadImage",
                      files = {
                          "sessionKey":(None, session_key),
                          "type":(None, "friend"),
                          "img": (file_path, f.read())
                      }
                  ).text)["url"]
              

              解决了解决了orz

              1 Reply Last reply Reply Quote 0
              • Topic has been marked as solved  M Michael1015198808 
              • 1 / 1
              • First post
                Last post
              Powered by Mamoe Technologies & NodeBB | 友情链接 | 服务监控 | Contact