Search code examples
pythonpython-3.xhttp-redirecthttp-request

python says http-response code is 200, but server says 301


I'm using python 3's urllib.request.urlopen() function. In the past it has worked fine. In fact, I used it a few month's ago for the same program and it worked fine. Now however, the server is logging 301 responses whenever I try to use an api to post on my site. when I use response.getcode() to find the response, it says it's 200.

What would cause this discrepancy? Is there another method to check if my request is failing, or a way to debug it? I personally don't have access to the server, but I can ask the admin to check them for me.

Thanks guys!

Edit

I've found the HTTP requests in wireshark. It sends a POST request, gets a 301 (text/html), then it sends a GET request and gets 200 (application/json). What does this mean? My original request was json (I used urllib.request.urlopen(url,data)). The first response for POST to http://sefaria.org/api/texts/Rashi_on_Berakhot.2a.1.1 is:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
    <head>
    <title>301 Moved Permanently</title>
    </head>
    <body>
        <h1>Moved Permanently</h1>
        <p>The document has moved <a href="http://www.sefaria.org/api/texts/Rashi_on_Berakhot.2a.1.1">here</a>.</p>\n
        <hr>
        <address>Apache/2.4.6 (Ubuntu) Server at sefaria.org Port 80</address>
    </body>
</html>

The GET request for that url responds with 200


Solution

  • So it turns out the I was posting to http://website.com instead of http://www.website.com... whoops.