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.

CommentFileSizeAuthor
#9 http-client-upload-file.png261.24 KBzipme_hkt

Issue fork eca_helper-3494034

Command icon 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:

  • 2.0.x Comparecompare

Comments

freelock created an issue. See original summary.

zipme_hkt’s picture

There 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

freelock’s picture

Ok, 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...

freelock’s picture

Options 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.

  • zipme_hkt committed 54a6c1d1 on 2.0.x
    Update #3494034 Add support for multipart files upload
    
zipme_hkt’s picture

I add multipart option for files upload. Please try check with dev release.

  • zipme_hkt committed 6ae6c552 on 2.0.x
    Update #3494034 Add support for multipart files upload
    

  • zipme_hkt committed c1805d55 on 2.0.x
    Update #3494034 Add support for multipart files upload
    
zipme_hkt’s picture

StatusFileSize
new261.24 KB

HTTP upload files

zipme_hkt’s picture

Version: 2.0.2-beta2 » 2.0.2
Status: Active » Fixed
zipme_hkt’s picture

Status: Fixed » Closed (fixed)
freelock’s picture

Finally 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...