Examples
A few common response headers:
Content-Type: text/html; charset=utf-8Content-Type: application/jsonContent-Type: application/xmlContent-Type: image/webpContent-Type: text/csv
Checking Content-Type before parsing a response in Python:
import requests
response = requests.get("https://example.com/api/products")
content_type = response.headers.get("Content-Type", "")
if "application/json" in content_type:
data = response.json()
print(data)
elif "text/html" in content_type:
html = response.text
print(html[:200])
else:
print(f"Unexpected content type: {content_type}")
Sending JSON with the correct request header:
curl -X POST "https://api.example.com/items" \
-H "Content-Type: application/json" \
-d '{"name":"widget","price":19.99}'
With ScrapeRouter, you still want to inspect the response type before deciding how to handle it:
import requests
response = requests.post(
"https://www.scraperouter.com/api/v1/scrape/",
headers={"Authorization": "Api-Key $api_key"},
json={"url": "https://example.com/feed.xml"}
)
content_type = response.headers.get("Content-Type", "")
print(content_type)
Practical tips
- Do not trust the URL alone: a page ending in
.jsoncan return HTML, and an endpoint that looked like HTML yesterday can start returning a bot check page today. - Check the header before parsing: especially if your scraper handles multiple targets or retries through different proxy/browser paths.
- Expect parameters: values often include extras like
charset=utf-8, so check forapplication/jsonrather than exact string equality. - Treat mismatches as signals: if you expected JSON and got
text/html, that often means rate limiting, a login wall, a WAF page, or a broken upstream. - Set it correctly on requests with a body: if you send JSON, use
Content-Type: application/json; if you upload form data, use the form content type the server expects. - Do not confuse it with
Accept:Content-Typedescribes the body you sent or received,Acceptdescribes what response formats you want. - Log it in production: when scrapers fail,
status code + content type + first 200 bytessaves a lot of guessing.
Use cases
- Choosing the right parser: send HTML to BeautifulSoup, JSON to
response.json(), XML to an XML parser, CSV to a CSV reader. - Detecting bot blocks: an API that should return
application/jsonsuddenly returningtext/htmlis a common sign you got a challenge page instead of data. - Validating uploads and POST requests: some endpoints fail silently or behave strangely if the request body format and
Content-Typeheader do not match. - Routing scraped content downstream: if you're storing pages, screenshots, feeds, and files in one pipeline,
Content-Typehelps decide where each response should go.