2

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.

Healsgood
  • 101
  • 2
  • 9

1 Answers1

2

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})
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • 1
    Thank you, I did not know about this and will keep it in mind in future. I've modified my code as you've suggested and it has produced a different error:

    404 - File or directory not found.

    The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.

    EDITED: Sorry, I was looking at the wrong test when I replied. I am given this error for all of my look ups. I will attempt to debug through it and find a resolution when I next get a chance. Cheers.
    – Healsgood Dec 02 '12 at 03:23