HTTP and redirect in web2py 🌐
HTTP and redirect in web2py play an important role HTTP raise the exceptions in web2py. Redirect in web2py accept an additional parameter which is optional and it redirects the user to the provided website.
web2py introduces only one new exception, HTTP. This exception can be generated at any point in a model, controller, or view with the following command:
raise HTTP(400, "Hello world")
It causes the control flow to return from the user's code to web2py and produce an HTTP response similar to:
HTTP/1.1 400 BAD REQUEST
Date: Fri, 05 Jul 2008 19:36:22 GMT
Server: Rocket WSGI Server
Content-Type: text/html
Via: 1.1 127.0.0.1:8000
Connection: close
Transfer-Encoding: chunked
Hello world
The HTTP status code is the first parameter in HTTP. The string that will be used as the body of the answer is the second parameter. The creation of the HTTP response header requires the usage of additional optional named parameters. Let’s see the creation of the HTTP response header with the example:
raise HTTP(400, 'Hello World', test=’header creation')
Output:
HTTP/1.1 400 BAD REQUEST
Date: Fri, 22 Jul 2022 19:36:22 GMT
Server: Rocket WSGI Server
Content-Type: text/html
Via: 1.1 127.0.0.1:8000
Connection: close
Transfer-Encoding: chunked
test: header creation
Hello World
Roll back the open database transaction before raising the exception if you don't want to commit it.
Only HTTP can be used for cross-page control flow. Other exceptions need to be captured by the application before web2py may ticket them.
Now let’s see the following command.
redirect(location)
is simply a shortcut for:
raise HTTP(303,
'You are being redirected <a href="%s">here</a>' % location,
Location='https://www.codingninjas.com')
The HTTP initializer method's named parameters are transformed into HTTP header directives, in this example, the redirection target location. Redirect accepts a second input that is optional: the HTTP status code for the redirection (303 by default). This number should be changed to 307 for a temporary redirect or 301 for a permanent redirect.
Frequently Asked Question
What is HTTP?
HTTP is an application-layer protocol used to transfer hypermedia content like HTML. HTTP protocol was designed for communication between web browsers and web servers, but it can also be used for other purposes.
What is a redirect?
A redirect can lead to any other URL; it does not have to be the same website. Redirects to another domain are sometimes known as cross-domain redirects.
What is the command for generating HTTP exceptions in web2py?
The command for generating HTTP exceptions in web2py is “raise HTTP(400, "Hello world")”.
What is web2py used for?
Web2py allows web developers to use Python to create dynamic online content. Web2py is intended to assist decrease time-consuming web development activities such as creating web forms from scratch, while a web developer may create a form from scratch if necessary.
Which companies are using the web2py framework?
Vidjil, Sarapis, StopStalk, Groupthink, Rune Interactive, Oceangrafix.com, and Food2Fork are a few famous companies using web2py.
Conclusion
In this article, we have extensively discussed HTTP and redirect in web2py. We have discussed what HTTP is and redirect in web2py and how to implement HTTP and redirect in web2py. If you would like to learn more, check out our articles on
Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc.
Enroll in our courses and refer to the mock test and problems available.
Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.
Happy Coding!