Is there a way to intercept HTTP requests made by Craft and modify the headers? #16126
-
I have a Feed Me feed which provides image URLs on a CDN path, the issue is the CDN is using Cloudflare and our server environment which happens to be Craft Cloud is being caught Cloudflare's bot/fingerprinting detection and the image URLs when being attempted to be downloaded are getting a 403. I've tested locally and found by specifying a User-Agent the request is successful. Unfortunately, for asset downloading in Feed Me there is no way to add/modify HTTP headers with Curl or fopen. I raised an issue on the Feed Me GitHub: craftcms/feed-me#1538 I was however wondering if there any way to use an event/app.web.php to intercept requests and add/modify headers depending on a condition i.e. hostname as a workaround. Feed Me I don't think provides any events specifically for asset fetching, so it's not really able to manipulate it, so I was wondering if I could do it somehow at the higher level. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can modify the config that Craft uses to create Guzzle (HTTP) clients via <?php
return [
'headers' => [
'User-Agent' => '...',
],
]; Looks like it’s not documented (on it), but Feed Me supports setting a <?php
return [
'*' => [
'clientOptions' => [
'headers' => [
'User-Agent' => '...',
],
],
],
]; |
Beta Was this translation helpful? Give feedback.
You can modify the config that Craft uses to create Guzzle (HTTP) clients via
config/guzzle.php
:Looks like it’s not documented (on it), but Feed Me supports setting a
clientOptions
array on your feed configs fromconfig/feed-me.php
, which will get merged into the main Guzzle config: