curl: (3) URL rejected: Malformed input to a URL function means curl could not parse what you gave it as a URL. In this test a literal space in the path caused it, and so did an empty shell variable in quotes. Encode the space as %20, and make sure the variable holds the URL before curl runs.

curl 8.5.0 printed this wording; the text "URL using bad/illegal format or missing URL" did not appear in these tests.

The error

$ curl -sS 'http://web:8080/my file.html'
curl: (3) URL rejected: Malformed input to a URL function
exit=3

Why it happens

The quotes kept the URL as one argument, so curl received http://web:8080/my file.html with a raw space in it, and refused it with exit code 3. The file my file.html did exist on the server, as the fix below shows, so the message says nothing about whether the file is there; it is about the text of the URL alone. Two more inputs below printed the same code 3 message, and a third printed a different one (code 2), so read the exact command that ran, after the shell has expanded its variables, before you look at the server.

An empty variable inside quotes gave the same message, because curl received an empty string:

$ URL=; curl -sS "$URL"
curl: (3) URL rejected: Malformed input to a URL function
exit=3
$ curl -sS ''
curl: (3) URL rejected: Malformed input to a URL function
exit=3

The same empty variable without quotes gave a different message and exit code, because then curl received no argument at all:

$ URL=; curl -sS $URL
curl: (2) no URL specified
curl: try 'curl --help' or 'curl --manual' for more information
exit=2

The fix

$ curl -sS 'http://web:8080/my%20file.html' -o /dev/null -w '%{http_code}\n'
200
exit=0

With the space written as %20, the server found the file and answered 200. For the variable case the fix is in the script that sets the variable: the captures show that an empty value reaches curl as an empty string (in quotes) or as no argument at all (without quotes), and neither is a URL. A guard for an empty variable was not part of these runs.

How it was reproduced

Reproduced inside ubuntu:24.04 containers on one user-defined Docker network on Docker engine 29.9.3, with curl 8.5.0 from apt in the client and bash as the shell. The server container, alias web, ran python3 -m http.server 8080 --directory /srv/www (Python 3.12.3) with a file named my file.html in that folder. The containers and the network were removed afterwards.

Frequently asked

What does curl: (3) URL rejected: Malformed input to a URL function mean?
curl could not parse the URL it was given. In this test a literal space in the path and an empty quoted variable both caused it.
How do I curl a URL with a space in it?
Encode the space as %20. Here 'http://web:8080/my%20file.html' returned 200 while the same URL with a raw space gave error 3.
Why does curl say no URL specified?
curl received no URL argument at all. In this test an empty variable written without quotes produced it, with exit code 2.

If the URL parses but the host name does not resolve, see curl: (6) Could not resolve host. More decoded errors in the Fixes category.