MiraiForum

    • 注册
    • 登录
    • 搜索
    • 热门
    • 最新
    • 未解决
    • 标签
    • 群组
    • 友情链接

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

    使用交流
    3
    4
    240
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • M
      Michael1015198808 最后由 编辑

      查阅文档之后看到需要以 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 条回复 最后回复 回复 引用 0
      • M
        Michael1015198808 最后由 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 条回复 最后回复 回复 引用 0
        • cssxsh
          cssxsh 最后由 编辑

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

          1 条回复 最后回复 回复 引用 0
          • D
            diyigemt 最后由 编辑

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

            1 条回复 最后回复 回复 引用 0
            • M
              Michael1015198808 最后由 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 条回复 最后回复 回复 引用 0
              • Topic has been marked as solved  M Michael1015198808 
              • 1 / 1
              • First post
                Last post
              Powered by Mamoe Technologies & NodeBB | 友情链接 | 服务监控 | Contact