How to upload files to slack using file.upload and requests

后端 未结 2 470
暗喜
暗喜 2021-02-06 08:59

I\'ve been searching a lot and I haven\'t found an answer to what I\'m looking for.

I\'m trying to upload a file from /tmp to slack using python requests bu

2条回答
  •  孤城傲影
    2021-02-06 09:47

    Sending files through http requires a bit more extra work than sending other data. You have to set content type and fetch the file and all that, so you can't just include it in the payload parameter in requests.

    You have to give your file information to the files parameter of the .post method so that it can add all the file transfer information to the request.

    my_file = {
      'file' : ('/tmp/myfile.pdf', open('/tmp/myfile.pdf', 'rb'), 'pdf')
    }
    
    payload={
      "filename":"myfile.pdf", 
      "token":token, 
      "channels":['#random'], 
    }
    
    r = requests.post("https://slack.com/api/files.upload", params=payload, files=my_file)
    

提交回复
热议问题