python upload string with https post

This question already has an answer here:

  • How are parameters sent in an HTTP POST request? 7 answers

  • When using the files parameter, requests creates the headers and body required to post files.
    If you don't want your request formated like that you can use the data parameter.

    url = 'http://httpbin.org/anything'
    headers = {'content-type': 'application/octet-stream'}
    files = {'file': 'ID,Namen1,test'}
    r = requests.post(url, data=files, headers=headers, auth=('user', 'password'))
    print(r.request.body)
    
    file=ID%2CName%0A1%2Ctest
    

    Note that when passing a dictionary to data it gets url-encoded. If you want to submit your data without any encoding you can use a string.

    url = 'http://httpbin.org/anything'
    headers = {'content-type': 'application/octet-stream'}
    files = 'ID,Namen1,test'
    r = requests.post(url, data=files, headers=headers, auth=('user', 'password'))
    print(r.request.body)
    
    ID,Name
    1,test
    
    链接地址: http://www.djcxy.com/p/41322.html

    上一篇: GET请求可以加书签,POST不能。 有人可以解释一下吗?

    下一篇: 用https post发送python上传字符串