Question

I've been working on a small pet project where I am using http.client to communicate (sorry if that's bad terminology, feel free to correct me) with omdbapi. From here I access the data on the website using the following code:

import http.client, json
__connection = http.client.HTTPConnection("www.omdbapi.com")

def getDetailsFromTitle(title):
    __connection.request("GET", "/?t=" + title)
    return __processRequest()

def getDetailsFromID(id):
     __connection.request("GET", "/?i=" + id)
     return __processRequest()

def __processRequest():
    try:
        response = __connection.getresponse()
        data = response.read()
        data = data.decode("utf-8")
        return json.loads(data)
except: return None

This worked fine for my first few trials, like I was able to get all my data back properly if I looked up say "Eureka", or "Superbad"; however, as soon as I inputted "Bad Kids Go To Hell" my try in __processRequest was being broken and None was being returned. The data I managed to get from the request was:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request</h2>
<hr><p>HTTP Error 400. The request is badly formed.</p>
</BODY></HTML>  

It's obviously breaking on the json.loads(data) as data isn't what is expected, but I'm not overly sure why I'm receiving this error. I went to the website and inputted "Bad Kids Go To Hell" and all worked fine.

Please let me know if you need anything more to assist me, Thank you.

Was it helpful?

Solution

To create the parameter lists of your urls, you should use [urlencode()][1]. In this case it's likely the spaces in the string that causes the problem, they should be converted to '+' characters.

__connection.request("GET", "/"+(urlencode({'t': title})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top