jalendport / craft-fetch Goto Github PK
View Code? Open in Web Editor NEWUtilise the Guzzle HTTP client from within your Craft templates.
License: MIT License
Utilise the Guzzle HTTP client from within your Craft templates.
License: MIT License
After updating to Craft 3.6.x along with it Guzzle was updated to 7.3. This plugin stopped working.
I created a proof of concept to help troubleshoot the issue.
# spun up a new project via
composer create-project craftcms/craft craft-latest
# added plugin
composer require jalendport/craft-fetch
# installed plugin via Control Panel > Settings → Plugins and click the “Install” button for Fetch.
Modified index.twig
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="utf-8" />
<title>three 66666666</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
<meta name="referrer" content="origin-when-cross-origin" />
</head>
<body>
{% set client = {
base_uri : 'https://api.chucknorris.io/jokes/random',
timeout : 30
} %}
{% set request = fetch(client, 'GET', null) %}
<pre><code>{{ dump(client) }}</code></pre>
<pre><code>{{ dump(request) }}</code></pre>
</body>
</html>
Results in
/home/vagrant/code/unraid/craft-fetch-tests/craft-latest/vendor/twig/twig/src/Extension/DebugExtension.php:61:
array (size=2)
'base_uri' => string 'https://api.chucknorris.io/jokes/random' (length=39)
'timeout' => int 30
/home/vagrant/code/unraid/craft-fetch-tests/craft-latest/vendor/twig/twig/src/Extension/DebugExtension.php:61:
array (size=2)
'error' => boolean true
'reason' => string 'URI must be a string or UriInterface' (length=36)
Not sure why it's returning this error.
If I then install Guzzle 6.x
composer require guzzlehttp/guzzle:^6.5.5
Plugin works again
/home/vagrant/code/unraid/craft-fetch-tests/craft-latest/vendor/twig/twig/src/Extension/DebugExtension.php:61:
array (size=2)
'base_uri' => string 'https://api.chucknorris.io/jokes/random' (length=39)
'timeout' => int 30
/home/vagrant/code/unraid/craft-fetch-tests/craft-latest/vendor/twig/twig/src/Extension/DebugExtension.php:61:
array (size=3)
'statusCode' => int 200
'reason' => string 'OK' (length=2)
'body' =>
array (size=7)
'categories' =>
array (size=0)
empty
'created_at' => string '2020-01-05 13:42:24.40636' (length=25)
'icon_url' => string 'https://assets.chucknorris.host/img/avatar/chuck-norris.png' (length=59)
'id' => string '_v6XFMafT_yXl7hxHKaVIg' (length=22)
'updated_at' => string '2020-01-05 13:42:24.40636' (length=25)
'url' => string 'https://api.chucknorris.io/jokes/_v6XFMafT_yXl7hxHKaVIg' (length=55)
'value' => string 'Chuck Norris took the dingo's baby.' (length=35)
Hey there! Not sure if this is something I'm doing wrong with the plugin or something I need to resolve outside of it so apologies if it's the latter.
I'm using Fetch (thanks!) and trying to get an image from an endpoint which in the docs, should return as a string. When I add the parameter 'false' I get a character blob (attached screenshot snippet). Without 'false', I get NULL returned. Is this what should be happening or is there something I can add to the options to pull this through?
Any help would be really appreciated.
The cURL request I'm trying to replicate with Fetch is:
curl -X GET "https://XXXXX/image/XXXXX/XXXXX/12345" -H "accept: image/jpeg" -H "Authorization: Basic XXXXX"
My current code is:
{% set imageOptions = {
headers : {
Authorization: ['Basic XXXXX'],
Accept: 'image/jpeg',
}
} %}
{% set image = fetch(client, 'GET', 'image/XXXXX/XXXXX/12345', imageOptions, false) %}`
I have tried to post my form values using the multipart option(https://docs.guzzlephp.org/en/stable/request-options.html#multipart), like the below in my twig template file,
{% set client = {
base_uri : 'baseURL',
timeout : 10
} %}
{% set destination = 'URLremianing part' %}
{% set options = {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer xxxx',
},
multipart:[ {
'name': 'title',
'contents': 'mr'
}]
} %}
getting the error "title" field is required.
Whereas, if I send using the JSON option(https://docs.guzzlephp.org/en/stable/request-options.html#json) it works perfectly,
instead of multipart in the above code, I replaced with,
json: {
'title': 'mr',
'first_name': '1'
}
Can someone please confirm multipart will work with form data fields?
Note : craft cms version(3.7.34)
It looks like this plugin is not supported with Craft 4. Will there be an update?
This plugin is not yet supported with Craft 5. Will there be an update?
Do you know if it is possible to send a post request to a built in craft controller with guzzle?
I want to use this plugin to fetch html content. Currently it passes the response body through a json_decode() function so it can only be used for fetching json data (this is not mentioned anywhere in the readme).
If I make the following modification to FetchTwigExtension.php then I can get the raw body response.
Would you consider making json decode optional?
$response = $client->request($method, $destination, $request);
$body = $response->getBody();
return [
'statusCode' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
'body' => $body->getContents()
];
Hi Luke,
I'm successfully using the plugin to retrieve a bearer token with basic auth, but I'm unsure as to how I should use this token in my next call.
After getting the token, here's what I've tried:
{% set client = {
base_uri : 'https://my.url.com',
timeout : 30
} %}
{% set options = {
headers : ['Authorization', 'Bearer: ' ~ myToken]
} %}
{% set results = fetch(client, 'POST', 'path/to/endpoint', options) %}
And this is the error:
Invalid or missing authorization head (truncated...)
If it's helpful, here's the instructions from the API on how to make that call. Appreciate any help you may have! Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.