O'Reilly logo

Python Requests Essentials by Bala Subrahmanyam Varanasi, Rakesh Vidya Chandra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Setting headers

HTTP header fields supply the necessary information about the request or response. We can mock any HTTP response header by using HTTPretty. To achieve that, we will be adding them as keyword arguments. We should keep in mind that the keys of the keyword arguments are always lower case and have underscores (_) instead of dashes.

For example, if we want to mock the server, which returns Content-Type, we can use the argument content_type. Do notice that, in the following part we are using an inexistent URL to showcase the syntax:

import httpretty
import requests

from sure import expect

@httpretty.activate
def setting_header_example():
    httpretty.register_uri(httpretty.GET,
                           "http://api.example.com/some/path",
 body='{"success": true}', ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required