1

I'm going to rewrite this POST request in python:

<?php
// Set these variables
$networkid = ""; // In your HasOffers system at Support -> API
$apikey = ""; // In your HasOffers system at Support -> API
$offerid = "0"; // Specify an offer ID to add the creative to
$creativetype = "image banner"; // Types: file, image banner, flash banner, email creative, offer thumbnail, text ad, html ad, hidden
$filename = "banner_728x90.gif"; // File name; no spaces, and file must be in same directory as this script
$display = $filename; // Or change this to the "display name" for this creative

// Don't change anything below here
$creativetype = urlencode($creativetype);
$display = urlencode($display);
$fields[$filename] = "@{$filename}";
$url = "http://api.hasoffers.com/v3/OfferFile.json?Method=create&NetworkToken={$apikey}&NetworkId={$networkid}&data[offer_id]={$offerid}&data[type]={$creativetype}&data[display]={$display}&return_object=1";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$resp = curl_exec($ch);
curl_close($ch);

print_r(json_decode($resp, true)); // Final output; remove or change this if you want
?>

As I know, in pycurl the CURLOPT_POSTFIELDS attribute is absent. What can I use instead?

Thank you!

1
  • Use requests in python. Commented May 23, 2016 at 6:27

1 Answer 1

1

there are many python libraries that can be used:

http.client

https://docs.python.org/3.1/library/http.client.html

requests

https://pypi.python.org/pypi/requests/

the commands are pretty straightforwards:

using http.client:

import http.client
conn = http.client.HTTPConnection("www.python.org")
conn.request("HEAD","/index.html")
res = conn.getresponse()
print(res.status, res.reason)
200 OK

or requests:

import requests
r = requests.get('https://github.com/timeline.json')
print r.json

Using HTTP client, a simple POST request may be done as follows:

connect = http.client.HTTPSConnection("base_url")
connect.request('POST', '/rest/api/'+ you_can_add_variables+ '/users?userEmail=' + another_variable, headers=headers)

Using requests:

import json
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}

r = requests.post(url, data=json.dumps(payload), headers=headers)

the documentation provided above should not prove difficult to understand

Sign up to request clarification or add additional context in comments.

1 Comment

could you give an example for post request, using these libraries? Setting for two parameters is enogh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.