Dieter
Dieter

Reputation: 441

How to do a x-http request (client) with Python

I am trying to reproduce a x-http request captured with Charles (Web Debugging Proxy) with Python but I can't find any documentation (or don't know what or where to look for).

Upvotes: 0

Views: 169

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124170

I'd use the requests library for this, as it makes tasks like these easier.

The request you captured seems to be posting JSON data, albeit with a text/javascript content type:

import requests
import json

headers = {'Content-Type': 'text/javascript;charset=utf-8')
data = json.dumps({'mod': 'calendar.field', 'action': 'mini', 'vars': {"current": 0}})
r = requests.post('http://www.kavka.be/xhttp.mod', data=data, headers=headers)

where data is a JSON string created from the same information as your proxy-captured POST.

Alternatively, if you only want to use the standard library, use urllib2:

import urllib2
import json

headers = {'Content-Type': 'text/javascript;charset=utf-8')
data = json.dumps({'mod': 'calendar.field', 'action': 'mini', 'vars': {"current": 0}})
req = urllib2.Request('http://www.kavka.be/xhttp.mod', data, headers)
r = urllib2.urlopen(req)

Upvotes: 2

Related Questions