curl icon indicating copy to clipboard operation
curl copied to clipboard

Request to Implement Alternate File Upload Name

Open the-mad-statter opened this issue 2 years ago • 9 comments

I would like to be able to perform the following (working) command line curl command using {curl} as the specific API I am uploading to does not otherwise support specifying a new filename:

CURL=`which curl`
$CURL -H "Accept: application/json" \
      -F "token=TOKEN_TOKEN" \
      -F "content=file" \
      -F "action=import" \
      -F "record=1" \
      -F "field=file_upload" \
      -F "[email protected];filename=new.txt" \
      https://somewhere.com/api/

I suspect this means somehow implementing curl_mime_filename which I do not think is currently supported.

If this is not of interest, any tips or pointers on how I might add this would be appreciated.

the-mad-statter avatar Jan 18 '23 22:01 the-mad-statter

We should probably combine this with porting the form.c implementation to the new curl_mime API. Right now we are still using the legacy form API.

jeroen avatar Jan 19 '23 13:01 jeroen

I figured out the old API was still being used when I started investigating how I might implement this myself. I am working on a solution using the old API for now and can submit a pull request if I get it working and that is of interest. I would commit to helping port form.c, but I don't know how fast I would get that done.

the-mad-statter avatar Jan 19 '23 15:01 the-mad-statter

It's not difficult but just a little messy if we want to support this for all cases in the old API.

Basically we need to add a CURLFORM_FILENAME flag to the relevant curl_formadd calls in form.c. But this legacy API makes it very difficult to set this flag conditionally, so I think we need even more if-else cases.

jeroen avatar Jan 19 '23 16:01 jeroen

Actually what you want can already be accomplished, simply by giving your file the correct name when you upload it?

Is there any reason your local file has to be called old.txt ? Maybe you can just copy or symlink it locally to new.txt and then upload it with curl::form_file()? That should automatically set the filename attribute on the form upload.

jeroen avatar Jan 19 '23 16:01 jeroen

The reason this issue came up is the I intend to run my code on Windows which does not allow some characters in file names such as colons. Therefore, I cannot just rename the file when uploading. Here is what I came up with. I think the if-else cases can be simplified by modifying curl::form_file() to include the alternate filename in the third position with a default of basename(path) and then reordering the elements to match what the C code expects.

the-mad-statter avatar Jan 19 '23 20:01 the-mad-statter

I think your solution crashes if a users sets name = NULL ?

jeroen avatar Jan 19 '23 21:01 jeroen

Ok, here are nested if-else cases encompassing the four scenarios.

the-mad-statter avatar Jan 19 '23 21:01 the-mad-statter

Will this suffice for a PR?

the-mad-statter avatar Jan 23 '23 15:01 the-mad-statter

Yeah I think that works. It's a lot of if-else but I guess there is no better way with the API. It will probably get easier when we port it to the new api.

jeroen avatar Jan 23 '23 16:01 jeroen