Calling a REST service using Python

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

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

I need to connect to it using POST. I tried reading it on the Internet ( here and here , for example) ... but this is my problem:

If I try to use the HTTP request request method by creating my own path, for example:

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

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

If I try to use the HTTP post request method, for example:

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 build params in the query string ("param1 = whatever & param2 = whatever" ...)

POST, , , ? ?

+3
3

, , , ... , REST, &key=value.

+5
  • urllib2
  • ; -

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

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

    .

+2

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 urlopenindicates that it is executing a POST request with empty data. quote_plusallows you to avoid special characters without having to enter the circuit &key=value.

0
source

Source: https://habr.com/ru/post/1761798/


All Articles