Python: Return 200 status code even for invalid urls

337 views Asked by At

So what I mean to say is that I want to send get/post requests to invalid url (e.g. https://this-is-a-fake-url.com) I know it will give error because url does not exist but I want to know a way so that it would give a 200 response code. So that if someone use wireshark or something to capture api requests, he would see many requests all having return code 200, no matter if the link is valid or not. Is it even possible? If so, please help :)

1

There are 1 answers

4
Zheng Bowen On

You can start a local server, and add it into hosts file. More specific, if you are on a linux machine:

  1. Run the following command, it will resolve this-is-a-fake-url.com to your localhost
sudo cat '127.0.0.1 this-is-a-fake-url.com' >> /etc/hosts
# In windows, write to C:\Windows\System32\drivers\etc
  1. Start a server on your local host
sudo python3 -m http.server
# Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
  1. Access your fake host
curl http://this-is-a-fake-url.com:8000 -v
# *   Trying 127.0.0.1:8000...
# * TCP_NODELAY set
# * Connected to this-is-a-fake-url.com (127.0.0.1) port 8000 (#0)
# > GET / HTTP/1.1
# > Host: this-is-a-fake-url.com:8000
# > User-Agent: curl/7.68.0
# > Accept: */*
# > 
# * Mark bundle as not supporting multiuse
# * HTTP 1.0, assume close after body
# < HTTP/1.0 200 OK
# ...