Fetch data from another source and modify request header dynamically

In my vcl_recv subroutine, I want to fetch data by calling some APIs from an external server and modify the original request header dynamically based on the fetched data. Is that possible? If yes, can someone show me the sample code that fetches data from a testing server via the following API call “https://dog.ceo/api/breeds/image/random

Thank you in advance

Debin

Hi @debinz

Thanks for reaching out.

I think I’m going to need some clarification as to what it is you’re trying to do, specifically with regards to the modification of the request header.

From what you’ve described, my understanding is that you want a request flow that follows a structure like this…

  • Incoming request received.
  • Lookup request in cache.
  • If in cache, return the response.
  • If not in cache, fetch content from origin server.
  • Fetch will be dependant on something in the request (i.e. a specific header).
  • Depending on something in the response (header?) modify the request header.
  • Restart the request using the newly modified header value.

By modifying the request header and then restarting the entire request flow, you’ll be able to change the call to the origin server.

Is this what you’re trying to achieve or is it something else entirely?

Ps, you can play around with the Fastly platform using https://fiddle.fastly.dev/ :slight_smile:

I think what @debinz may be trying to achieve is a pattern we often call a “preflight”. This is a common technique in which you “park” the original request, make a call to an API (such as a user profile data, A/B testing or authentication endpoint) and then annotate the original request with the data you get from that API call. The original request, with the additional headers can then be processed normally and go to origin if there is no hit in cache.

Here are some examples of that:

1 Like

Ah! yes of course :slight_smile:

That was kinda the use case I had in my mind but I just couldn’t recall it properly.

Nice, thanks :+1: