---
title: "How to Use Headers with Python Requests: A Beginner-Friendly Tutorial"
description: "Learn how to use Python requests headers in your web scraping or automation projects, from importing requests library to sending correct HTTP requests."
url: "https://iproyal.com/blog/python-requests-headers-tutorial/"
---

- [EN - English](https://iproyal.com/)
   
  [中文 - Chinese](https://iproyal.cn/)

[Book a Demo](https://iproyal.com/request-a-demo/) [Create Account](https://dashboard.iproyal.com/register/) [Create Account](https://dashboard.iproyal.com/register/) [Login](https://dashboard.iproyal.com/login/) [Login](https://dashboard.iproyal.com/login/)

[Book a Demo](https://iproyal.com/request-a-demo/)

[Back to blog](https://iproyal.com/blog/)

# How to Use Headers with Python Requests: A Beginner-Friendly Tutorial

[Vilius Dumcius](https://iproyal.com/blog/authors/vilius-dumcius/)

[Last updated - September 8, 2025 ‐ 7 min read](https://iproyal.com/blog/authors/vilius-dumcius/)

[Tutorials](https://iproyal.com/blog/category/tutorials/)

## Key Takeaways

- The Python requests library allows you to control the requests easily while building web scrapers or other automation projects

- Common Python requests headers are **User-Agent**, **Accept, Accept-Language, Referer, -Content-Type**, and **Authorization**.

- GET requests are used to retrieve data, and POST requests send data to the server. They tend to use different headers for their tasks.

- The Python requests library allows you to view and customize headers easily.

Using correct request headers is one of the most important parts of a [Python web scraping ](https://iproyal.com/blog/python-web-scraping-step-by-step-guide/)project. **Sending incorrect or malformed headers may bring error status codes or trigger anti-bot measures.&#x20;**&#x41;void such hurdles by learning how to send Python request headers correctly.

You’ll find that using the [Python requests library ](https://iproyal.com/blog/python-requests-library/)is quite straightforward when you’re already experienced. **If you’re only taking your first steps in Python, there are some crucial basics to cover.**

## What Are HTTP Headers and Why They Matter

**The Hypertext Transfer Protocol (HTTP) defines that every request and response must include HTTP headers, showing the web server how to process it.&#x20;**&#x57;e can think of them as shipping labels for physical deliveries specifying how to handle the package, where to send it, and other crucial information.

**When a Python script or a browser sends a HTTP request, headers tell the web server who you are, what you’re sending, and what kind of response is expected.&#x20;**&#x54;here’s more to [HTTP headers ](https://iproyal.com/blog/what-are-http-headers/), but such is the basic role of request headers we’re concerned here.

**The Python *requests* library is one of the most popular Python libraries, allowing users to send automated HTTP requests easily.&#x20;**&#x57;ith *requests,* headers like *User-Agent* are sent all at once in a single synchronous request, and the script waits for the server’s response before continuing.

**For example, the *User-Agent* header informs that you’re using Chrome and waits for whether such a browser type is accepted.&#x20;**&#x41;ll other request headers are sent at the same time and receive a response synchronously.

More advanced web scraping or automation projects might require [HTTPX or AIOHTTP ](https://iproyal.com/blog/httpx-vs-aiohttp-vs-requests/)request libraries. **They can send Python request headers asynchronously or mix synchronous and asynchronous methods.**

## Common Header Fields and What They Look Like

Header fields are key:value pairs sent and received by the client and server with every HTTP response and request. **They contain the metadata needed for the communication.&#x20;**&#x57;hen we speak of using Python requests headers correctly, usually we mean common header fields.

| Header Name | What It Does | Example Value | Python Format |
| :-: | :-: | :-: | :-: |
| User-Agent | Identifies the client (browser or script) making the request. | Mozilla/5.0 (Windows NT 10.0; Win64; x64) | ‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64…) |
| Accept | Specifies the content type(s) the client expects. | text/html,application/json | ‘Accept’: ‘text/html,application/json’ |
| Accept-Language | Indicates preferred language(s) for the response. | en-US,en;q=0.9 | ‘Accept-Language’: ‘en-US,en;q=0.9’ |
| Referer | Tells the server which URL referred you to the current request. | `https://www.google.com` | ‘Referer’: `https://www.google.com` |
| Content-Type | Tells the server the media type of the request body (used in POST requests). | application/json | ‘Content-Type’: ‘application/json’ |
| Authorization | Sends credentials or API keys for authentication. | Bearer YOUR\_API\_TOKEN | ‘Authorization’: ‘Bearer YOUR\_API\_TOKEN’ |

Some proxy or bot detection methods go beyond *User-Agent* or, in the case of API requests, Authorization headers. **Uncommon or special headers like *Forwarded,* *X-Forwarded-For,* or *Via* are sometimes checked as well.**

**If they are present or misconfigured, they allow websites to track users, revealing their proxy usage or other identifying information.&#x20;**&#x49;n such cases, [proxy headers tests ](https://iproyal.com/proxy-headers-test/)become handy as they allow you to test whether your proxy server can be identified by such header values.

## Differences Between GET and POST Headers

GET and POST are two of the most commonly used HTTP methods, especially when building web scrapers or automating other tasks.

- **A GET request is used to retrieve&#x20;**&#x64;ata from a server. It typically sends data through the URL as a query parameter.
- **A [POST request ](https://iproyal.com/blog/post-requests-with-python/)is used to send&#x20;**&#x64;ata to the server. The JSON data payloads are commonly placed in the body of the request.

Since GET and POST methods differ in purpose, the Python requests headers they use differ as well. **GET requests can use only *User-Agent* and *Accept* headers. POST requests need to pass more information, with Content-Type request headers often mandatory.**

| Header | GET Request | POST Request | Example Value |
| :-: | :-: | :-: | :-: |
| User-Agent | Often | Often | Mozilla/5.0 |
| Accept | Common | Common | text/html, application/json |
| Accept-Language | Common | Rare | en-US,en;q=0.9 |
| Referer | Common | Often | `https://www.google.com` |
| Content-Type | Rare | Mandatory when sending body data | application/json |
| Authorization | For APIs or logins | For APIs or logins | Bearer YOUR\_API\_TOKEN |

Some *Accept* headers, such as *Accept-Language,* are irrelevant for sending structured data via POST. **In API requests, both methods might require an authorization header since the server needs to know whether to allow access.**

## Installing and Importing the Requests Library

**To start using Python requests headers, you’ll need to install the Python requests library.&#x20;**&#x46;irst, open a terminal (you might need admin permissions) to install it.

- **On Windows,&#x20;**&#x70;ress Windows button + X. Select terminal (or press the letter I).
- **On macOS,&#x20;**&#x70;ress Command (⌘) + Spacebar and enter the terminal.
- **On Linux (Ubuntu),&#x20;**&#x50;ress CTRL+Alt+T.

Alternatively, most developers use IDEs and virtual environments. **For Python development Visual Studio Code or PyCharm work wonders.&#x20;**&#x49;f you choose these options (recommended), use the terminal within those IDEs to get libraries:

Type in the following command in the terminal.

```javascript
pip install requests
```

Alternatively, you could install a Python *requests* library for only one user.

```javascript
pip install --user requests
```

Or make a system-wide installation.

```javascript
sudo pip install requests
```

To confirm that everything is working, we can **make a simple GET request to test a URL.**

```python
import requests

response = requests.get('https://httpbin.org/get')
print(response.status_code)
print(response.text)
```

If the Status code is 200, **your request was successful, and the Python requests library is set up properly.&#x20;**&#x54;he URL we provided in this snippet is made for such tests and will return JSON data about your request. Here's how the response might look like.

```json
200
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.25.1",
    "X-Amzn-Trace-Id": "Root=1-12345678-123456789012345678901234"
  },
  "origin": "123.45.67.89",
  "url": "https://httpbin.org/get"
}
```

In a real-world scenario, you are likely to encounter many more possible problems with Python requests. **Be sure to implement a [failed requests retry strategy ](https://iproyal.com/blog/python-requests-retry/)to avoid issues like connection timeouts, server downtimes, or blocks.**

## How to View and Print Response Headers

The previous code example used a very basic way of displaying HTTP request headers. When building a Python scraper, integrating APIs, or doing other tasks, **it's best to label the headers and print each on a separate line.**

They can include content type headers, server details, rate limits, caching rules, and other details. **The Python requests library allows us to access them using the *.headers* attribute on a response object.&#x20;**&#x48;ere's how you can do it.

```python
import requests

response = requests.get('https://httpbin.org/get')

print("Status code:", response.status_code)

print("\nResponse headers:")
for header, value in response.headers.items():
    print(f"{header}: {value}")
```

The output now will be **each header in a separate line.&#x20;**&#x49;t's useful in real-world scenarios to check whether the server is returning JSON data, finding rate-limiting headers, or other cases.

```typescript
Status code: 200

Response headers:
Content-Type: application/json
Date: Mon, 05 Aug 2026 12:34:56 GMT
Server: gunicorn/19.9.0
Content-Length: 234
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Connection: keep-alive
```

Since *headers* functions as a dictionary, **it allows access to specific headers separately.**

```python
import requests

response = requests.get('https://httpbin.org/get')
content_type = response.headers['Content-Type']

print("Content-Type:", content_type)
```

After running such code, you would see output similar to the following.

```javascript
Content-Type: application/json
```

## How to Add Headers to a GET and POST Request

Now that we know how to view headers, another practical problem to solve is sending custom headers to outgoing requests. **This is important when calling APIs or accessing websites with a web scraper or automation tool.**

In the Python's *requests* library, **custom headers are passed as a dictionary using the *headers=* parameter in functions like *requests.get()* and *requests.post().*&#x20;**&#x48;ere's how to set up a custom *User-Agent* in a GET request.

```python
import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}

response = requests.get('https://httpbin.org/headers', headers=headers)
print(response.json())
```

This code snippet sends a custom *User-Agent* header with a specifically defined identity. **It's important when you want your *User-Agent* to mimic a specific setup to avoid bans or other restrictions.**

Similarly, POST requests can be customized as well by specifying the *Content-Type: application/json.*

```python
import requests
import json

headers = {
    'Content-Type': 'application/json'
}

data = {
    'username': 'testuser',
    'password': 'secure123'
}

response = requests.post('https://httpbin.org/post', headers=headers, data=json.dumps(data))
print(response.json())
```

**This custom Python requests header informs the web server that you are sending JSON data, which is required in many instances.&#x20;**&#x41;dding custom headers is crucial for communicating with the server and gaining access.

## Conclusion

The examples we covered here should give you a head start when using Python requests headers and customizing them. The next step is to start using [proxies with the Python requests ](https://iproyal.com/blog/how-to-use-iproyal-proxies-with-python-requests/)library before integrating the code into your automation or web scraping project.

[Create Account](https://dashboard.iproyal.com/)

[Author](https://iproyal.com/blog/authors/vilius-dumcius/)

[Vilius Dumcius](https://iproyal.com/blog/authors/vilius-dumcius/)

[Head of Engineering](https://iproyal.com/blog/authors/vilius-dumcius/)

With six years of programming experience, Vilius specializes in full-stack web development with PHP (Laravel), MySQL, Docker, Vue.js, and Typescript. Managing a skilled team at IPRoyal for years, he excels in overseeing diverse web projects and custom solutions. Vilius plays a critical role in managing proxy-related tasks for the company, serving as the lead programmer involved in every aspect of the business. Outside of his professional duties, Vilius channels his passion for personal and professional growth, balancing his tech expertise with a commitment to continuous improvement.

[Learn More About Vilius Dumcius ](https://iproyal.com/blog/authors/vilius-dumcius/)[Meet all Writers](https://iproyal.com/blog/authors/)

Share on

### In This Article

- [What Are HTTP Headers and Why They Matter](https://iproyal.com/blog/python-requests-headers-tutorial/#What-Are-HTTP-Headers-and-Why-They-Matter)
- [Common Header Fields and What They Look Like](https://iproyal.com/blog/python-requests-headers-tutorial/#Common-Header-Fields-and-What-They-Look-Like)
- [Differences Between GET and POST Headers](https://iproyal.com/blog/python-requests-headers-tutorial/#Differences-Between-GET-and-POST-Headers)
- [Installing and Importing the Requests Library](https://iproyal.com/blog/python-requests-headers-tutorial/#Installing-and-Importing-the-Requests-Library)
- [How to View and Print Response Headers](https://iproyal.com/blog/python-requests-headers-tutorial/#How-to-View-and-Print-Response-Headers)
- [How to Add Headers to a GET and POST Request](https://iproyal.com/blog/python-requests-headers-tutorial/#How-to-Add-Headers-to-a-GET-and-POST-Request)
- [Conclusion](https://iproyal.com/blog/python-requests-headers-tutorial/#Conclusion)

Ready to get started?

[Register now](https://dashboard.iproyal.com/register/)

Data News in Your Inbox

No spam whatsoever, just pure data gathering news, trending topics and useful links. Unsubscribe anytime.

No spam. Unsubscribe anytime.

## Related articles

[View all](https://iproyal.com/blog/category/tutorials/)

[Tutorials](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

## [The Most Common Payment Processing Obstacles and Solutions for SaaS Companies](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

[Here are the most common payment processing obstacles SaaS companies face and the solution that helped us focus on growth and our services!](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

[August 8, 2022](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

[3 min read](https://iproyal.com/blog/the-most-common-payment-processing-obstacles-and-solutions-for-saas-companies/)

[Tutorials](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

## [Easy Programmatic SEO with WordPress](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

[Our fourth and final entry in the pSEO series explains how to combine programmatic SEO with WordPress using plugins or code snippets.](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

[March 30, 2023](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

[10 min read](https://iproyal.com/blog/easy-programmatic-seo-with-wordpress/)

[Tutorials](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)

## [API for Dummies — The Coder’s and No-coder’s Guide](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)

[Learn API basics with this guide for coders and non-coders. Discover the different components of an API call.](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)

[June 1, 2023](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)

[11 min read](https://iproyal.com/blog/api-for-dummies-the-coders-and-no-coders-guide/)
