Hi, Iam trying to post a form with cookies using Requests module.
When I post the form and see the page with the results, there is an error in the page, saying that I didnt fill one field right. Iam absolute sure I fill that field right. When I post the data with web browser, its all right.
I think it must be cookie problem, becose when I disable the cookies in the browser and post the right data, it shows the exact same error when I send it via Python.

There is the code:

# -*- coding: utf-8 -*-
import urllib, urllib2
import cookielib
import requests

adress= 'http://....'
param= {
    'name' : '....',
    'email' : '....',
    'phone' : '....',
    'message' : '....'}#etc... 

headers = {
      'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1',
      'Connection': 'keep-alive',
      'Accept-Language': 'en-us,en;q=0.5',
      'Accept-Encoding': 'gzip, deflate',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }

c=requests.session()
requestp = c.get(adress)
SSID=requestp.cookies['PHPSESSID']
cookiesp = dict(PHPSESSID=SSID)
request = c.post(adress, data=param, headers=headers)

print request.headers          #headers of the result webpage
print request.request.headers  #headers posted

Output:

{'content-length': '6911', 'x-powered-by': 'PHP/5.3.17-1~dotdeb.0', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'server': 'nginx/1.2.4', 'connection': 'keep-alive', 'pragma': 'no-cache', 'cache-control': 'max-age=200', 'date': 'Thu, 01 Nov 2012 19:44:14 GMT', 'content-type': 'application/xhtml+xml; charset=utf-8'}
{'Accept-Language': 'en-us,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1', 'Cookie': 'PHPSESSID=2des6ks39kouoqvn3u6l425kp6', 'Content-Type': 'application/x-www-form-urlencoded'}

If you have same ideas what is wrong, I would so gratefull. Thank you very much and I apologise for my english (:

Ok, I solved this with mechanize :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.