requests的post提交form-data; boundary=????

陈知
2023-12-01

提交这种用boundary分隔的表单数据时,有两种方法,一种是以传入files参数,另一种是传入data参数,data参数需要自己用boundary来分隔为指定的形式,而files参数则以元组的形式传入,代码如下

files = {
    'TOKENKEY': (None, str(nowtime)), # 当前时间戳
    'wbuploadpath': (None, ""),
    'owner':  (None, "886166885"),
    'token': (None, token),
    'params': (None, 'eyJjb252ZXJ0cGFyYW1zIjpbeyJjb252ZXJ0VHlwZSI6ImltYWdldG90aHVtYiIsInBhcmFtcyI6eyJpbWdhdXRvcmVzaXplIjoidHJ1ZSIsImltZ2F1dG9yZXNpemV3aWR0aCI6IjEwMjQiLCJpbWdhdXRvcmVzaXplaGVpZ2h0IjoiMTAwMDAiLCJsaW1pdHdpZHRoIjoiMTYyMCIsImxpbWl0aGVpZ2h0IjoiMTAwMDAiLCJpbWd0aHVtYndpZHRoIjoiNjQwIiwiaW1ndGh1bWJoZWlnaHQiOiIxMDAwMDAiLCJsaW1pdGZpbGVzaXplIjoiMTA0ODU3NiJ9fSx7ImNvbnZlcnRUeXBlIjoiaW1hZ2V0b3JlZHVjZXF1YWxpdHkiLCJwYXJhbXMiOnsiaW1nYXV0b3Jlc2l6ZSI6InRydWUiLCJpbWdhdXRvcmVzaXpld2lkdGgiOiIxMDI0IiwiaW1nYXV0b3Jlc2l6ZWhlaWdodCI6IjEwMDAwIiwibGltaXR3aWR0aCI6IjE2MjAiLCJsaW1pdGhlaWdodCI6IjEwMDAwIiwiaW1ndGh1bWJ3aWR0aCI6IjY0MCIsImltZ3RodW1iaGVpZ2h0IjoiMTAwMDAwIiwibGltaXRmaWxlc2l6ZSI6IjEwNDg1NzYifX1dLCJhbGxvd2V4dHMiOiJqcGcsanBlZyxnaWYscG5nLGJtcCxnaWYscG5nLGRvYyxkb2N4LHBwdCxwcHR4LGV4Y2VsLHhscyx4bHN4LGJtcCxqcGcsanBlZyx6aXAscmFyIiwidXB0b2tlbiI6IjgxNjAzOTRCREE4NjRDRDZCRDlBRUUwNTY0OEUwRkNGIiwibm9kZWNvZGUiOiJsb2NhbCJ9' ),
    'uploadfilejson': (None, ""),
    'watermark': (None, ""),
    'waterlocal': (None, ""),
    'waterfilename': (None, ""),
    'waterurl': (None, ""),
    'classselect': (None, '1160'),
    'wbtitle': (None, wbtitle),
    'image': (None, "", 'application/octet-stream'),
    'wbcontent': (None, wbcontent),
    'yanzheng': (None, str(yanzheng))
}
data = """
        ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="TOKENKEY"
    
    {}
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="wbuploadpath"
    
    
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="owner"
    
    886166885
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="token"
    
    {}
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="params"
    
    eyJjb252ZXJ0cGFyYW1zIjpbeyJjb252ZXJ0VHlwZSI6ImltYWdldG90aHVtYiIsInBhcmFtcyI6eyJpbWdhdXRvcmVzaXplIjoidHJ1ZSIsImltZ2F1dG9yZXNpemV3aWR0aCI6IjEwMjQiLCJpbWdhdXRvcmVzaXplaGVpZ2h0IjoiMTAwMDAiLCJsaW1pdHdpZHRoIjoiMTYyMCIsImxpbWl0aGVpZ2h0IjoiMTAwMDAiLCJpbWd0aHVtYndpZHRoIjoiNjQwIiwiaW1ndGh1bWJoZWlnaHQiOiIxMDAwMDAiLCJsaW1pdGZpbGVzaXplIjoiMTA0ODU3NiJ9fSx7ImNvbnZlcnRUeXBlIjoiaW1hZ2V0b3JlZHVjZXF1YWxpdHkiLCJwYXJhbXMiOnsiaW1nYXV0b3Jlc2l6ZSI6InRydWUiLCJpbWdhdXRvcmVzaXpld2lkdGgiOiIxMDI0IiwiaW1nYXV0b3Jlc2l6ZWhlaWdodCI6IjEwMDAwIiwibGltaXR3aWR0aCI6IjE2MjAiLCJsaW1pdGhlaWdodCI6IjEwMDAwIiwiaW1ndGh1bWJ3aWR0aCI6IjY0MCIsImltZ3RodW1iaGVpZ2h0IjoiMTAwMDAwIiwibGltaXRmaWxlc2l6ZSI6IjEwNDg1NzYifX1dLCJhbGxvd2V4dHMiOiJqcGcsanBlZyxnaWYscG5nLGJtcCxnaWYscG5nLGRvYyxkb2N4LHBwdCxwcHR4LGV4Y2VsLHhscyx4bHN4LGJtcCxqcGcsanBlZyx6aXAscmFyIiwidXB0b2tlbiI6IjRBMDVGQzUwMjQzQzRGNkY4NTQ1QjQ4QjhGOTNCQzg5Iiwibm9kZWNvZGUiOiJsb2NhbCJ9
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="uploadfilejson"
    
    
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="watermark"
    
    
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="waterlocal"
    
    
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="waterfilename"
    
    
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="waterurl"
    
    
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="classselect"
    
    1160
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="wbtitle"
    
    {}
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="image"; filename=""
    Content-Type: application/octet-stream
    
    
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="wbcontent"
    
    {}
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2
    Content-Disposition: form-data; name="yanzheng"
    
    {}
    ------WebKitFormBoundaryYuBOtqhxWuNB9YP2--
    
    """.format(str(nowtime), token, wbtitle, wbcontent, yanzheng).encode('UTF-8')
    #response = requests.post(url, headers=headers, data=data)
    response = requests.post(url, headers=headers, files=files)

 

 类似资料: