Empty response.history when using requests module in Zapier Code step

I’m working on a Zapier automation and need to track URL redirections using the requests library in a Code by Zapier action. My goal is to capture the redirect history, but I keep getting empty results.

Here’s what I’m trying:

import requests
req = requests.get('https://example.com', allow_redirects=True)
req.raise_for_status()

return [{'redirect_history': req.history}]

No matter what URL I test with, the history attribute comes back empty even though I know some URLs should redirect. The requests library is supposed to be supported in Zapier’s environment according to their documentation.

Is there something specific about how Zapier handles the requests module that would cause this issue? Or am I missing something in my implementation?

I ran into this exact same issue about six months ago - super frustrating! The problem is Zapier’s Code step can’t handle complex objects in the output. The requests library works fine, but those Response objects in req.history have too much internal stuff that Zapier can’t serialize. When you try returning them directly, Zapier just strips them out and you get nothing. You need to loop through the history and pull out only the basic data you actually want before returning anything. Just grab the URL strings and status codes as integers - those simple data types work perfectly with Zapier.

This happens because Zapier can’t serialize the Response objects in req.history to JSON - that’s why you’re getting empty output. I ran into the exact same issue a few months back. The fix is to manually extract what you need from each response instead of returning the raw history object. Loop through req.history and grab the specific attributes you want like status codes and URLs. Here’s what worked for me: history_data = [] for resp in req.history: history_data.append({'status_code': resp.status_code, 'url': resp.url}) Then return history_data instead of req.history directly. Zapier can properly serialize this and you’ll actually see the redirect chain in your output.

Yeah, classic Zapier problem - not a requests bug. Zapier can’t handle complex history objects in the output. Convert them to simple dictionaries first: [{'url': str(r.url), 'status': r.status_code} for r in req.history]. Works way better than dumping raw response objects.