I have to POST a request to a server. In the API documentation of the website there is this example that uses cURL in PHP:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.website.com');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "request=$wrapper");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
;
$data = curl_exec($ch);
curl_close($ch);
But my app is done using Python, so I tried to write something like that but this code doesn’t work:
req = urllib2.Request(url, formatted)
response = urllib2.urlopen(req)
html = response.read()
print html+"\n\n"
Can you help me write a working conversion of a PHP cURL program to Python?
Thank you!!
It’s quite embarrassing but… the only problem with my code that uses urllib and urllib2 is… that this code do a GET and not a POST !!!
Here my scan using Wireshark:
1- using urllib and urllib2
2- using PyCurl
so the code works, but it’s not right for me because i need a POST, but i will prefer to use NOT PyCurl.
Any idea?
Thank you very much!!