1. 程式人生 > >request的post 接口上傳文件

request的post 接口上傳文件

nbsp filesize text name str span files 地址 val

前言:註冊接口需要上次頭像,fiddle抓的接口如圖,這個時候就需要用到:files

技術分享圖片

2,舉例說明

  a:有一個上傳接口,地址如下:http://xx.xx.xx.xx//upload/stream

  b:上傳接口的參數如下所示:{"parentId":"","fileCategory":"personal","fileSize":179,"fileName":"summer_text_0920.txt","uoType":1}

3、發送請求,代碼如下所示:

header={"ct":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"}
files = {
file:open(D:\\test_data\\summer_test_data_05.txt,rb)}#此處是重點!我們操作文件上傳的時候,把目標文件以open打開,然後存儲到變量file裏面存到一個字典裏面 upload_data={"parentId":"","fileCategory":"personal","fileSize":179,"fileName":"summer_text_0920.txt","uoType":1} upload_res=requests.post(upload_url,upload_data,files=files,headers=header)#
#此處是重點!我們操作文件上傳的時候,接口請求參數直接存到upload_data變量裏面,在請求的時候,直接作為數據傳遞過去

4、運行結果

技術分享圖片

request的post 接口上傳文件