How to fetch as client?

Hello, I am using to worker to fetch http request.

await fetch(request_url, init);    

But I want to do fetch as client like reverse proxy. How can I done this ?
For example :
When user “A” call a worker fetch request , “fetch” needs to be represent as User “A” and go to destination url as fetched by User “A” 's network and metadata.

Your question is not overly clear.

fetch() will simply request the given URL in the context of Cloudflare’s proxy servers. What do you mean by “as client”?

When user “A” call a worker fetch request , “fetch” needs to be represent as User “A” and go to destination url as fetched by User “A” 's network and metadata.

In that case you can’t perform the request on Cloudflare’s side but need to do so in the user’s browser.

Please let me clear the conclusion.
Is it correct to say that Cloudflare Workers’ fetch method cannot support to add headers like “user-agents” and “referer” inside fetch options? please.

No, you should be all right setting these headers, but that is not what you asked for in your last response.

If it is just about these two headers, then you can simply set them in your request. https://developers.cloudflare.com/workers/examples/ has examples.

But the original request object has those fields anyhow.

If you just want to pass the clients original request headers etc. then you can do so like this.

await fetch(request_url, request)

N.B if you then also want to also add other headers to the raw request you will need to have cloned the request first, but from your description i think this would be sufficient.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.