Problem/Motivation
I'm using the http client action in a model to post to Mastodon, and this is working. Now I want to upload an image, with alt text.
Proposed resolution
Add a "File" serialization that uses Guzzle's "multipart" support to post a file to a remote service.
User interface changes
Need to provide documentation for how to pass in the file to upload, as well as other metadata about the file.
Using the Mastodon service as an example, we want to post 3 items in the multipart section:
"name" - "file" -- the field name for the multi-part form. Mastodon expects this to be "file" but other APIs might need a different field name.
"contents" - a file or stream pointer -- this is what Guzzle requires to have the file contents or a handle.
"description" - Mastodon uses this as an alt tag for the uploaded image.
API changes
To get this working, I've added an option to the "data_serialization" field, for "file". When that is selected, the "data" field is run through the YAML parser - the value of "file" is passed into fopen, and the value of "description" is passed to description.
Data model changes
Right now, no other changes to the data model are needed -- but if there are other values to pass to Guzzle's multipart uploads (e.g. pass other field values, pass multiple files) may need some improvements to the UI and data model.
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | http-client-upload-file.png | 261.24 KB | zipme_hkt |
Issue fork eca_helper-3494034
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
zipme_hkt commentedThere is Options field allow you input any Guzzle's client options. Please try it.
Other way you can try new feature in last release called Eca Helper Quick Action allow you write small php code and implement your action in the file sites/eca/EcaActions.php
Comment #3
freelockOk, I'm not exactly sure how I messed up the MR, but you can at least see the direction I'm going. With this, I'm setting the data value to:
file: "[entity:field_door_image:entity:path]"
description: "[entity:field_door_image:alt]"
and it's successfully posting an image.
For more, see the post that's automatically publishing in a few hours: https://www.freelock.com/advent/2024/automatically-post-mastodon-or-othe...
Comment #4
freelockOptions doesn't work for files, at least not binary ones.
If there's unrecognizable characters, the Yaml parser throws an exception, and you're done.
Guzzle takes one of:
- A file pointer (created by fopen)
- A stream wrapper
- The raw file contents
I don't see any way of getting the first 2 into a token, and the third chokes the Yaml parser.
Comment #6
zipme_hkt commentedI add multipart option for files upload. Please try check with dev release.
Comment #9
zipme_hkt commentedComment #10
zipme_hkt commentedComment #11
zipme_hkt commentedComment #12
freelockFinally got a chance to test this, and spent hours trying to figure out why it didn't work.
I'm not exactly sure why, but the "data_yaml" config form setting is not saving correctly on my instance. It always comes up blank, and if I set it to "yes" it remains 0 in the underlying config.
I'm trying to track this down, because I don't see any obvious issues -- the form element is set to be a checklist but it's rendering as a select with yes/no values, so I think something is overriding the form rendering for this element for some reason. Will file an issue when I know what it is...