Contact Blog Consulting

RPC response headers in Google App Engine

To make an asynchronous request with Google App Engine you’ll likely use the urlfetch library with a callback like this:

import logging
import functools
from google.appengine.api import urlfetch

urls = ['https://api1.example.com', 'https://api2.example.com']

# Define callback.
def log_html(rpc):
    html = rpc.response.content
    logging.info(html)

# Request each url with callback.
rpcs = []
for url in urls:
    rpc = urlfetch.create_rpc()
    rpc.callback = functools.partial(log_html, rpc)
    urlfetch.make_fetch_call(rpc, url)
    rpcs.append(rpc)

# Wait until callbacks have run.
for rpc in rpcs:
    rpc.wait()

Getting the response content of the request is easy: rpc.response.content, but I couldn’t find documentation for parsing the headers.

Turns out they’re stored as rpc.response.header_list in Protobuf format. You can convert to a normal dictrionary with:

def get_rpc_headers(rpc):
	return {h.key(): h.value() for h in rpc.response.header_list()}