17

I am trying to use urllib2.urlopen to perform GET and POST requests via the Facebook Graph API. I noticed from here: https://stackoverflow.com/questions/2690723/facebook-graph-api-and-django that I can perform GET request fairly easily.

And from here: How to send a POST request using django? and the Python docs http://docs.python.org/library/urllib2.html it seems that it needs the data param to perform a POST request.

But looking at the Facebook's API: http://developers.facebook.com/docs/reference/api/event/#invited it says

You can invite users to an event by issuing an HTTP POST to /EVENT_ID/invited/USER_ID

I am not sure how I could do that with urlopen, since opening this url directly is going to only check if the user has been invited, as mentioned on the API page:

You can check whether a specific user has been invited to an event by issuing an HTTP GET to /EVENT_ID/invited/USER_ID:

Appreciate the input.

Community
  • 1
  • 1
airfang
  • 1,238
  • 3
  • 14
  • 22

4 Answers4

38

It sounds like you want to send an empty POST request, even though urllib2.urlopen() only sends a post when you specify the data parameter.

It seems like it actually sends an empty POST if you set data="", and GET request only when data=None:

urllib2.urlopen("http://127.0.0.1:8000", data="")
"POST / HTTP/1.1" 501 - 

urllib2.urlopen("http://127.0.0.1:8000", data=None)
"GET / HTTP/1.1" 200 -

Hope that helps. I got the response printouts from the little HTTP server they have an example for here: http://docs.python.org/library/simplehttpserver.html

Christian
  • 2,426
  • 16
  • 10
2

Another way to send an empty POST is to create a Request and override its get_method. This is more work in this case, but may be cleaner in other cases (if you already have a Request, for example).

    request = urllib2.Request(url)
    request.get_method = lambda: 'POST'
    urllib2.urlopen(request)

get_method is a method that returns the method to be used, so here we override it with a lambda function that returns 'POST'.

scjody
  • 959
  • 2
  • 7
  • 12
2

Directly from Python (urllib2) Documentation

urllib2.urlopen(url[, data][, timeout]) Open the URL url, which can be either a string or a Request object. Warning HTTPS requests do not do any verification of the server’s certificate. data may be a string specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided.

Shashank
  • 21
  • 1
0

I think you've pretty much answered your own question, you're just over-thinking it...

  • To invite a user issue a POST with urlopen

  • To check if a user has been invited, issue a GET with urlopen.

In both cases, you are communicating with the same URL, the difference is GET or POST.

If you are unsure of the difference between a GET and a POST with urlopen, read this document:

http://www.doughellmann.com/PyMOTW/urllib2/

Gil Birman
  • 35,242
  • 14
  • 75
  • 119
  • Thanks for the answer, I am aware of the difference. But since urlopen only sends a POST request when the data parameter is supplied, I didn't know what to do if I don't have any parameter (http://developers.facebook.com/docs/reference/api/ has some cases with POST actions without arguments) – airfang Mar 03 '12 at 02:27
  • so for GET don't use data, just supply the parameters in the URL. – Gil Birman Mar 03 '12 at 03:50