Equivalent Python code for the following Java http get requests
Question: I am trying to convert the following Java code to Python. Not sure what I am
doing wrong, but I end up with an internal server error 500 with python.

Is the "body" in `httplib.httpConnection` method equivalent to Java
`httpentity`? Any other thoughts on what could be wrong? The input information
I collect is correct for sure.

Any help will be really appreciated. I have tried several things, but end up
with the same internal server error.

Java Code:

    
    
    HttpEntity reqEntitiy = new StringEntity("loginTicket="+ticket);
    HttpRequestBase request = reMethod.getRequest(uri, reqEntitiy);
    request.addHeader("ticket", ticket);
    HttpResponse response = httpclient.execute(request);
    HttpEntity responseEntity = response.getEntity();
    StatusLine responseStatus = response.getStatusLine();
    

Python code:

    
    
     url = serverURL + "resources/slmservices/templates/"+templateId+"/options"
    #Create the request
    ticket = ticket.replace("'",'"')
    headers = {"ticket":ticket}
    print "ticket",ticket
    
    reqEntity = "loginTicket="+ticket
    body = "loginTicket="+ticket
    
    
    url2 = urlparse.urlparse(serverURL)
    h1 = httplib.HTTPConnection(url2.hostname,8580)
    print "h1",h1
    
    url3 = urlparse.urlparse(url)
    print "url path",url3.path
    ubody = {"loginTicket":ticket}
    data = urllib.urlencode(ubody)
    conn = h1.request("GET",url3.path,data,headers)
    #conn = h1.request("GET",url3.path)
    response = h1.getresponse()
    lines = response.read()
    print "response.status",response.status
    print "response.reason",response.reason
    


Answer: You don't need to go this low level. Using `urllib2` instead:

    
    
    import urllib2
    from urllib import urlencode
    
    url = "{}resources/slmservices/templates/{}/options".format(
        serverURL, templateId)
    headers = {"ticket": ticket}
    params = {"loginTicket": ticket}
    url = '{}?{}'.format(url, urlencode(params))
    
    request = urllib2.Request(url, headers=headers)
    response = urllib2.urlopen(request)
    print 'Status', response.getcode()
    print 'Response data', response.read()
    

Note that the parameters are added to the _URL_ to form URL query parameters.

You can do this simpler still by installing the [`requests`
library](http://docs.python-requests.org/en/latest/):

    
    
    import requests
    
    url = "{}resources/slmservices/templates/{}/options".format(
        serverURL, templateId)
    headers = {"ticket": ticket}
    params = {"loginTicket": ticket}
    
    response = requests.get(url, params=params, headers=headers)
    print 'Status', response.status
    print 'Response data', response.content  # or response.text for Unicode
    

Here `requests` takes care of URL-encoding the URL query string parameters and
adding it to the URL for you, just like Java does.

