Skip to content

Scripting-friendly uploads: auto-detect curl/Wget/HTTPie and return plain-text URL (#170) - #377

Open
presto8 wants to merge 4 commits into
szabodanika:masterfrom
presto8:feature/url-only-after-upload
Open

presto8 wants to merge 4 commits into
szabodanika:masterfrom
presto8:feature/url-only-after-upload

Conversation

@presto8

@presto8 presto8 commented Nov 12, 2026

Copy link
Copy Markdown

What

Makes /upload script-friendly for command-line clients, closing #170.

  • Added a url_only_after_upload multipart field. When set, the server responds 200 OK with Content-Type: text/plain and just the resulting upload URL, instead of the normal 302 redirect to the upload page.
  • The server now also auto-detects command-line clients (curl, Wget, HTTPie) via User-Agent and returns the same plain-text response automatically, with no extra flag needed for the common case.
  • url_only_after_upload is tri-state (Option<bool>): unset defers to the User-Agent auto-detection, while an explicit true/false always overrides it — so a real curl request can still force the normal redirect (e.g. for testing), and non-CLI/unrecognized clients can still opt in explicitly.
  • Works across all privacy levels, including server-encrypted uploads, and honors MICROBIN_SHORT_PATH / MICROBIN_PUBLIC_PATH the same way the "Copy URL" button does, falling back to the incoming request's scheme/host otherwise.
  • Docs: new "Using MicroBin from the console" section in templates/guide.html with curl examples (file upload, text upload, forcing the response mode either way, and a sample shell function).

Example

# Just works, no flags needed:
curl -F "content=@file.txt" https://microbin.example.com/upload
# -> https://microbin.example.com/upload/pig-dog-cat

# Explicit opt-in/opt-out also available:
curl -F "content=@file.txt" -F "url_only_after_upload=true"  https://microbin.example.com/upload
curl -F "content=@file.txt" -F "url_only_after_upload=false" https://microbin.example.com/upload

Testing

  • cargo build succeeds.
  • Manually tested: text content, file attachment, and private/encrypted uploads all return a clean plain-text URL automatically from curl/Wget/HTTPie-style requests, normal browser requests are unaffected, and explicit url_only_after_upload=true/false overrides the auto-detection in both directions.

Marked as draft pending maintainer review of the approach (in particular the User-Agent based auto-detection, which is a heuristic and not spoof-proof, though it only affects response formatting, nothing security-sensitive). Mirror of presto8#1.

Preston Hunt added 3 commits November 11, 2026 12:19
Addresses szabodanika#170: curl-based uploads currently get a
302 redirect, which forces every shell one-liner to scrape the
Location header. Setting url_only_after_upload=true on the /upload
multipart form now makes the server respond 200 OK with just the
resulting upload URL as plain text, for every privacy level (public,
unlisted, read-only and server-encrypted private).

The URL follows the same short_path/public_path rules as the browser
'Copy URL' button, falling back to the incoming request's scheme/host
when neither is configured, so it works out of the box without extra
server configuration.

Also documents curl-based uploads (file and content fields, avoiding
the argument-list-too-long pitfall) and the new flag in the guide
page.
…flag

Command-line clients are now detected from the User-Agent header, so a
plain upload already gets the scripting-friendly 200 OK + plain-text URL
response instead of the browser redirect. url_only_after_upload=true is
still available as an explicit opt-in for clients with an unrecognized
or missing User-Agent.
…tion

Previously the field only worked as an opt-in (OR'd with the curl/Wget/
HTTPie User-Agent auto-detection), so url_only_after_upload=false had no
effect for an actual command-line client: the auto-detection would still
force the plain-text URL response.

url_only_after_upload is now parsed as Option<bool>: unset defers to the
User-Agent heuristic as before, while an explicit true or false always
wins, letting any client (including real curl) force either response
mode for testing or integration purposes.
@presto8
presto8 marked this pull request as ready for review November 12, 2026 02:41
Build on rust:1-bookworm to match the distroless cc-debian12 runtime
(rust:1 is now Debian 13, whose glibc is newer than the runtime's), and
link liblzma statically via LZMA_API_STATIC since the distroless image
does not ship it. Add a .dockerignore to keep target/, .git, local data
and .env files out of the build context.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant