Fixing Python requests.exception.ConnectionError: connection aborted “BadStatusLine”

Python requests.exception.ConnectionError: connection aborted “BadStatusLine” is an error which occurs sometimes when you use a faulty URL with python requests or when you are not using the correct syntax.

I will explain why this error takes place and how to fix it, while also trying to add other solutions that could solve the error.

Exploring the Error : Python requests.exception.ConnectionError: connection aborted “BadStatusLine”

This is an error which occurs sometimes when you use a faulty URL with python requests or when you are not using the correct syntax.

Do not mix between errors. Make sure the error message looks like the error message bellow after double checking.

                                                                       #
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",
))
                                                                       #

Check out the sections bellow for a number of tested solutions that I have tried and that have worked for me.

The method that solved my problem : use https instead of http. Try payload={} and if it does not work try json={}

Like many python errors, there might be a hidden reason of why the error occurs for some people, but for the most part it is either a faulty URL or using the wrong syntax with python requests.

First of all, I recommend using Payload just like this

                                                                       #
payload={...}
userloginurl="http://myurl:port/uid"
r=requests.get(userloginurl,params=payload)
print(r.url)
                                                                       #

You should of course replace myurl and port in your code.

But even ten, you will still get the error if you are using the wrong URL. For many people the error occurs because their connection should be established using https instead of http.

                                                                       #
# use this 
userloginurl="https://myurl:port/uid"
# instead of this
userloginurl="http://myurl:port/uid"
                                                                       #

If the error persists try using json instead of payload

                                                                       #
# use this 
json={...}
userloginurl="https://myurl:port/uid"
# instead of this
payload={...}
userloginurl="http://myurl:port/uid"
                                                                       #

I believe the options above are enough to solve the issue, thank you for reading, cheers.

Summing-up : 

We arrived at the end of this quest to solve this annoying error : Python requests.exception.ConnectionError connection aborted BadStatusLine , I hope me sharing my experience with you helped, I hope the other solutions helped, If you like this website support us on Kofi and keep browsing.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/