Using CURL I can post a file like
CURL -X POST -d "pxeconfig=`cat boot.txt`" https://ip:8443/tftp/syslinux
My file looks like
$ cat boot.txt
line 1
line 2
line 3
I am trying to achieve the same thing using requests module in python
r=requests.post(url, files={'pxeconfig': open('boot.txt','rb')})
When I open the file on server side, the file contains
{:filename=>"boot.txt", :type=>nil, :name=>"pxeconfig",
:tempfile=>#<Tempfile:/tmp/RackMultipart20170405-19742-1cylrpm.txt>,
:head=>"Content-Disposition: form-data; name=\"pxeconfig\";
filename=\"boot.txt\"\r\n"}
Please suggest how I can achieve this.
In the request body, click "form-data", hover over the "key" input field, and find the hidden dropdown that says "Text". Click "Text", and then change it to say "File". In the "Value" field, click "Select File" and select the file to send via the POST request body.
To send a POST request using the Python Requests Library, you should call the requests. post() method and pass the target URL as the first parameter and the POST data with the data= parameter.
Method 1: Using the Python's os Module: Also, the enctype attribute with "multi-part/form-data" value will help the HTML form to upload a file. Lastly, we need the input tag with the filename attribute to upload the file we want. Lastly, we need the input tag with the filename attribute to upload the file we want.
Your curl request sends the file contents as form data, as opposed to an actual file! You probably want something like
with open('boot.txt', 'rb') as f:
r = requests.post(url, data={'pxeconfig': f.read()})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With