views:

62

answers:

3

I have a REST service that I'm trying to call. It requires something similar to the following syntax:

http://someServerName:8080/projectName/service/serviceName/
      param1Name/param1/param2Name/param2

I have to connect to it using POST. I've tried reading up on it online (here and here, for example)... but this is my problem:

If I try using the HTTP get request method, by building my own path, like this:

BASE_PATH = "http://someServerName:8080/projectName/service/serviceName/"
urllib.urlopen(BASE_PATH + "param1/" + param1 + "/param2/" + param2)

it gives me an error saying that GET is not allowed.

If I try using the HTTP post request method, like this:

params = { "param1" : param1, "param2" : param2 }
urllib.urlopen(BASE_PATH, urllib.urlencode(params))

it returns a 404 error along with the message The requested resource () is not available. And when I debug this, it seems to be building the params into a query string ("param1=whatever&param2=whatever"...)

How can I use POST but pass the parameters delimited by slashes as it's expected? What am I doing wrong?

A: 
  1. Use urllib2
  2. You're going to have to be clever; something like

    params = { "param1" : param1, "param2" : param2 }

    urllib2.urlopen(BASE_PATH + "?" + urllib.urlencode(params), " ")

    Might work.

Jason Scheirer
+1: You must provide data to force it to be a POST request.
S.Lott
Why should I use urllib2? And why the question mark?
froadie
A: 

Something like this might work:

param1, param2 = urllib.quote_plus(param1), urllib.quote_plus(param2)
BASE_PATH = "http://someServerName:8080/projectName/service/serviceName/"
urllib.urlopen(BASE_PATH + "param1/" + param1 + "/param2/" + param2, " ")

The second argument to urlopen tells it to do a POST request, with empty data. The quote_plus allows you to escape special characters without being forced into the &key=value scheme.

carl
This doesn't seem to work... :( I'm getting the same "requested resource is not available" error as before
froadie
are you sure you are able to access the resource? do you need to authenticate yourself first? are you able to give us the url so we can test?
JiminyCricket
A: 

I know this is sort of unfair, but this is what ended up happening... The programmer in charge of the REST service changed it to use the &key=value syntax.

froadie