3

I'm attempting to post the data of a pop-out form to a local web site. To do this I'm emulating the requests header and data and cookie information provided by the site. (Note: I am largely redacting my email and password from the code (for obvious reasons), but all other code will remain the same.)

I have tried mulitple permutations of the cookie, header, requests, data, etc. Additionally, I have verified in a network inspector the cookie and expected headers and data. I am able to easily set a cookie using requests' sample code. I cannot explain why my code won't work on a live site, and I'd be very grateful for any assistance. Please see the following code for further details.

import requests
import robobrowser
import json

br = robobrowser.RoboBrowser(user_agent="Windows Chrome",history=True)

url = "http://posting.cityweekly.net/gyrobase/API/Login/CookieV2"
data ={"passwordChallengeResponse":"....._SYGwbDLkSyU5gYKGg",
        "email": "<email>%40bu.edu",
        "ttl":"129600",
        "sessionOnly": "1"
        }
headers = {
    "Origin": "http://posting.cityweekly.net",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en-US,en;q=0.8,ru;q=0.6",
    "User-Agent": "Windows Chrome", #"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    "Referer": "http://posting.cityweekly.net/utah/Events/AddEvent",
    "X-Requested-With": "XMLHttpRequest",
    "Connection": "keep-alive",
    "Cache-Control": "max-age=0",
    "Host":"posting.cityweekly.net"

}
cookie = {"Cookie": "__utma=25975215.1299783561.1416894918.1416894918.1416897574.2; __utmc=25975215; __utmz=25975215.1416894918.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __qca=P0-2083194243-1416894918675; __gads=ID=e3b24038c9228b00:T=1416894918:S=ALNI_MY7ewizuxK0oISnqPJWlLDAeKFMmw; _cb_ls=1; _chartbeat2=D6vh2H_ZbNJDycc-t.1416894962025.1416897589974.1; __utmb=25975215.3.10.1416897574; __utmt=1"}
r = br.session.get(url, data=json.dumps(data), cookies=cookie, headers=headers)
print r.headers
print [item for item in r.cookies.__dict__.items()]

Note that I print the cookies object and that the cookies attribute (a dictionary) is empty.

Bee Smears
  • 803
  • 3
  • 12
  • 22

1 Answers1

1

You need to perform a POST to login to the site. Once you do that, I believe the cookies will then have the correct values, (not 100% on that...). This post clarifies how to properly set cookies.

Note: I don't think you need to do the additional import of requests unless you're using it outside of RoboBrowser.

Community
  • 1
  • 1
Patrick
  • 171
  • 1
  • 7
  • Welcome to StackOverflow. Your answer has ended up in the "Very Low Quality Posts Review" queue, because it looks like a comment seeking clarification rather than being an actual answer. However, I can see that you have provided the actual answer: they need to use POST. You might want to rephrase your answer to make it look less like a question or a comment, lest someone inadvertently delete it. – GreenAsJade Dec 26 '14 at 04:03