Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Python HTTP Request with POST data and Cookie?

I am trying to do a HTTP POST using cookies in Python.

I have the values of URL, POST data and cookie.

import urllib2
url="http://localhost/testing/posting.php"
data="subject=Alice-subject&addbbcode18=%23444444&addbbcode20=0&helpbox=Close+all+open+bbCode+tags&message=alice-body&poll_title=&add_poll_option_text=&poll_length=&mode=newtopic&sid=5b2e663a3d724cc873053e7ca0f59bd0&f=1&post=Submit"
cookie = "phpbb2mysql_data=a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3A1%3A%223%22%3B%7D; phpbb2mysql_t=a%3A9%3A%7Bi%3A3%3Bi%3A1330156986%3Bi%3A1%3Bi%3A1330160737%3Bi%3A5%3Bi%3A1330161702%3Bi%3A6%3Bi%3A1330179284%3Bi%3A2%3Bi%3A1330160743%3Bi%3A7%3Bi%3A1330163187%3Bi%3A8%3Bi%3A1330164442%3Bi%3A9%3Bi%3A1330164739%3Bi%3A10%3Bi%3A1330176335%3B%7D; phpbb2mysql_sid=5b2e663a3d724cc873053e7ca0f59bd0"
#creating HTTP Req
req = urllib2.Request(url,data,cookie)

f = urllib2.urlopen(req)
print f.read()

However, if I try to run the program, it is throwing an error:

Traceback (most recent call last):
  File "task-4.py", line 7, in <module>
    req = urllib2.Request(url,data,cookie)
  File "/usr/lib/python2.6/urllib2.py", line 197, in __init__
    for key, value in headers.items():
AttributeError: 'str' object has no attribute 'items'

I have two questions: 1. Is my HTTP POST request proper? (I have properly been able to execute the same thing in Java and got a HTTP 200 with a successful post to phpBB, however, I am new to Python) 2. Can someone show me a toy example of handling HTTP POST with POST data and cookies?

Thanks in advance,

Roy

like image 747
user916315 Avatar asked Feb 25 '12 15:02

user916315


People also ask

How do I send a cookie in a POST request in Python?

To send a request with a Cookie, you need to add the "Cookie: name=value" header to your request. To send multiple cookies in a single Cookie header, separate them with semicolons or add multiple "Cookie: name=value" request headers.

How do I send cookies in a POST request?

To send cookies to the server, you need to add the "Cookie: name=value" header to your request. To send multiple Cookies in one cookie header, you can separate them with semicolons. In this Send Cookies example, we are sending HTTP cookies to the ReqBin echo URL.

How do you send data in a Python POST request?

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.

How do I add a cookie to a request?

To add cookies to a request for authentication, use the header object that is passed to the get/sendRequest functions. Only the cookie name and value should be set this way. The other pieces of the cookie (domain, path, and so on) are set automatically based on the URL the request is made against.


2 Answers

You can try requests, which makes life easier when dealing with HTTP queries.

import requests
url="http://localhost/testing/posting.php"
data= {
    'subject': 'Alice-subject',
    'addbbcode18': '%23444444',
    'addbbcode20': '0',
    'helpbox': 'Close all open bbCode tags',
    'message': 'alice-body',
    'poll_title': '',
    'add_poll_option_text': '',
    'poll_length': '',
    'mode': 'newtopic',
    'sid': '5b2e663a3d724cc873053e7ca0f59bd0',
    'f': '1',
    'post': 'Submit',
    }
 cookies = {'phpbb2mysql_data': 'a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3A1%3A%223%22%3B%7D',
    'phpbb2mysql_t': 'a%3A9%3A%7Bi%3A3%3Bi%3A1330156986%3Bi%3A1%3Bi%3A1330160737%3Bi%3A5%3Bi%3A1330161702%3Bi%3A6%3Bi%3A1330179284%3Bi%3A2%3Bi%3A1330160743%3Bi%3A7%3Bi%3A1330163187%3Bi%3A8%3Bi%3A1330164442%3Bi%3A9%3Bi%3A1330164739%3Bi%3A10%3Bi%3A1330176335%3B%7D', 
    'phpbb2mysql_sid': '5b2e663a3d724cc873053e7ca0f59bd0',
    }
print requests.get(url, data=data, cookies=cookies).text

http://python-requests.org/

like image 190
Valentin Lorentz Avatar answered Sep 28 '22 13:09

Valentin Lorentz


the 3rd argument you pass is a header and should be a dictionary. This should do it

cookie = {"Cookie" : "phpbb2mysql_data=a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3A1%3A%223%22%3B%7D; phpbb2mysql_t=a%3A9%3A%7Bi%3A3%3Bi%3A1330156986%3Bi%3A1%3Bi%3A1330160737%3Bi%3A5%3Bi%3A1330161702%3Bi%3A6%3Bi%3A1330179284%3Bi%3A2%3Bi%3A1330160743%3Bi%3A7%3Bi%3A1330163187%3Bi%3A8%3Bi%3A1330164442%3Bi%3A9%3Bi%3A1330164739%3Bi%3A10%3Bi%3A1330176335%3B%7D; phpbb2mysql_sid=5b2e663a3d724cc873053e7ca0f59bd0"}
like image 36
vikki Avatar answered Sep 28 '22 13:09

vikki