Need help in accessing REST API using httplib

This is the place for queries that don't fit in any of the other categories.

Need help in accessing REST API using httplib

Postby Technocrat » Fri Dec 06, 2013 5:50 pm

Hi everyone,

I will first explain you what I am exactly trying to do.

I have created a web service in .net & hosted it on an IIS server. The web service has been exposed using REST API url. URL is of the form http://host:port/methodname?var1=lo (POST method).
I am accessing this service using python module "httplib" . The parameters are passed in the following way.
Code: Select all
params = {'var1' : 'lo' }
params =urllib.urlencode(params)

trial = httplib.HTTPConnection("host","port")
trial.request("POST",url , params, headers)

But by using the above method I am able to access the service, but the query parameters are not getting passed.
I will really appreciate any help.
Last edited by stranac on Fri Dec 06, 2013 6:18 pm, edited 1 time in total.
Reason: Added code tags. Locked.
Technocrat
 
Posts: 3
Joined: Fri Dec 06, 2013 5:31 pm

Re: Need help in accessing REST API using httplib

Postby micseydel » Sat Dec 07, 2013 12:16 am

I can do more thorough testing of this later, when I'm in front of a computer I can put a simple flask server on for testing purposes, but in the mean time, have you tried urllib2 where you give it a URL as you would a browser? For that matter, does a regular browser request work with your server as you expect?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1223
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Need help in accessing REST API using httplib

Postby Technocrat » Sat Dec 07, 2013 2:00 pm

Thanks Micseydel for your reply.
I haven't tried with urllib2 but I am getting the expected work done when I tried the mentioned url through REST Client in Mozilla.
My web service expects HTTP request url in the form of http://host:port/methodname?var1=lo but when I use this url with httplib to instantiate
httplib.HTTPConnection it throws error 400 BAD REQUEST.
And I guess that's correct as httplib wants you to send the query parameters as a dictionary like I shared the sample code earlier.
Could you please help me in understanding how httplib is internally forming the url & hitting the REST API or some other way to get my task accomplished.
Technocrat
 
Posts: 3
Joined: Fri Dec 06, 2013 5:31 pm

Re: Need help in accessing REST API using httplib

Postby micseydel » Sun Dec 08, 2013 9:09 pm

I realized as I was trying to test it (using a local Flask server) that your orignal code is incomplete. You really should be providing a runnable amount of code, or at least as runnable as possible. For example, I don't mind throwing together a simple Flask server to echo a request back, but I don't want to guess what your "url" and "headers" variables are.

That said, we really should know what's going on the server side too. If you can provide the documentation for the end point that would be fantastic, and the source code wouldn't hurt although I don't know C# so hope we don't know it.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1223
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Need help in accessing REST API using httplib

Postby Technocrat » Wed Dec 11, 2013 10:37 am

Thanks Micseydel.

I have resolved the error. I guess the documentation for httplib is wrong when you need to pass query parameters in URL.
http://docs.python.org/2/library/httplib.html
What they have mentioned is like u form the dictionary for parameters & then pass it like you have body.
Code: Select all
>>> import httplib, urllib
>>> params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
...            "Accept": "text/plain"}
>>> conn = httplib.HTTPConnection("bugs.python.org")
>>> conn.request("POST", "", params, headers)
>>> response = conn.getresponse()
>>> print response.status, response.reason
302 Found
>>> data = response.read()
>>> data
'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
>>> conn.close()
This way I was getting 400 BAD REQUEST error , then I figured out it has to be passed like this:
Code: Select all
>>> conn = httplib.HTTPConnection("bugs.python.org")
>>> conn.request("POST",URL + "?" +  params, "",headers)

What's your opinion about this ?
Technocrat
 
Posts: 3
Joined: Fri Dec 06, 2013 5:31 pm

Re: Need help in accessing REST API using httplib

Postby micseydel » Wed Dec 11, 2013 6:51 pm

I would agree that either the documentation is wrong/unclear, or the module itself could have been better designed.

IIRC, you would use httplib because you're making multiple requests on the same connection a lot. This would be the only reason to prefer it over urllib/urllib2. So if performance isn't an issue here, I recommend urllib2, and if it is, then I'm glad you worked out your problem in spite of the difficulty.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1223
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Need help in accessing REST API using httplib

Postby XndrK » Mon Dec 16, 2013 8:55 pm

May I ask what this is doing in the Challenges section?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Need help in accessing REST API using httplib

Postby micseydel » Mon Dec 16, 2013 11:12 pm

XndrK wrote:May I ask what this is doing in the Challenges section?

It shouldn't be. I'll move it right now.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1223
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests