0

Why there is different response in python and ruby mechanize for the same HTTP GET request?

Python:


def send(cls, url, values = None, method = 'GET', header={}):
    print(url)
    c = cls()
    data = None
    context = ssl._create_unverified_context()
    if values is not None:
        # data = urllib.parse.urlencode(values)
        # data = data.encode('utf-8') # data should be bytes
        data = json.dumps(values)
        data = data.encode('utf-8')

    if data is None:
        c.request = urllib.request.Request(url)
    else:
        c.request = urllib.request.Request(url, data)
        c.request.add_header('Content-Type', 'application/json')
    for key, value in header.items():
        c.request.add_header(key,value)

    c.request.method = method
    # print(c.request.get_header('Content-Type'))
    try:
        c.response = urllib.request.urlopen(c.request, context=context)
    except urllib.error.HTTPError as e:
        c.response = e
        # print(e.hdrs)

    return c

resp = HTTPSender.send("https://10.5.110.143/__api__/logon/704383e332d46036369a510e2673fe94?method=144", method="GET")
print(resp.body)

Output :

{ "message": "The method is not allowed for the requested URL." }

Ruby mechanize:

@agent=Mechanize.new
$resp = .get("https://10.5.110.143/__api__/logon/704383e332d46036369a510e2673fe94?method=144")
puts $resp.content

Output:

405 => Net::HTTPMethodNotAllowed for https://10.5.110.143/api/logon/9c7ca3f5 34ea1c8a5bfe64d7cf08797a?method=144 -- unhandled response

Is it something expected?

owgitt
  • 313
  • 5
  • 22
  • I hate comment like this, but it would be better to use `requests` library. It will reduce amount of code related to making request to one or two lines. – Andrii Rusanov Feb 26 '16 at 08:19
  • Do you mean that the ruby version raised Exception and python version not? – realli Feb 26 '16 at 08:26
  • Yes. Python returned a nice output. But not ruby mechanize. – owgitt Feb 26 '16 at 08:27
  • @ShanthaDodmane, you catched the exception in your code, maybe try to print the Exception there – realli Feb 26 '16 at 08:37
  • If I try to print the exception, this is what it prints : 405 => Net::HTTPMethodNotAllowed for https://10.5.110.143/__api__/logon/d5bfba14 212b0e0bb84b5ece9a02d122?method=144 -- unhandled response – owgitt Feb 26 '16 at 10:17

0 Answers0