How to send a “multipart/form-data” with requests in python?

前端 未结 9 1809
野趣味
野趣味 2020-11-22 01:29

How to send a multipart/form-data with requests in python? How to send a file, I understand, but how to send the form data by this method can not understand.

9条回答
  •  故里飘歌
    2020-11-22 02:02

    I'm trying to send a request to URL_server with request module in python 3. This works for me:

    # -*- coding: utf-8 *-*
    import json, requests
    
    URL_SERVER_TO_POST_DATA = "URL_to_send_POST_request"
    HEADERS = {"Content-Type" : "multipart/form-data;"}
    
    def getPointsCC_Function():
      file_data = {
          'var1': (None, "valueOfYourVariable_1"),
          'var2': (None, "valueOfYourVariable_2")
      }
    
      try:
        resElastic = requests.post(URL_GET_BALANCE, files=file_data)
        res = resElastic.json()
      except Exception as e:
        print(e)
    
      print (json.dumps(res, indent=4, sort_keys=True))
    
    getPointsCC_Function()
    

    Where:

    • URL_SERVER_TO_POST_DATA = Server where we going to send data
    • HEADERS = Headers sended
    • file_data = Params sended

提交回复
热议问题