Fix curl URL Rejected: Malformed Input Error
The curl error “URL rejected: Malformed input to a URL function” means curl could not parse a URL, usually because of invalid characters, an incorrectly assembled address, or an extra command-line argument being treated as a URL. Fix the exact value curl receives, not just the address as it appears in your editor. Start by inspecting the expanded URL, then correct its quoting, encoding, or argument placement.
Use the symptom to choose your first check:
| Symptom | Likely Cause | First Fix |
|---|---|---|
| A copied URL fails immediately | Hidden whitespace, control characters, or literal quotation marks | Inspect the URL’s actual contents |
| The command breaks around query parameters | Shell interpretation of characters such as & |
Quote the complete URL argument |
| A quoted URL still fails when it contains spaces | Shell quoting has not URL-encoded the data | Encode the affected path or query value |
| The command works manually but fails in a script or plugin | Empty variables, unresolved placeholders, or incorrect URL construction | Inspect the final assembled URL |
| A download succeeds, followed by a malformed-URL error | A destination path or another stray argument is being treated as an additional URL | Correct the output arguments |
| A command copied from Linux fails in PowerShell | Different executable resolution or shell syntax | Invoke curl.exe and use PowerShell-compatible syntax |
What the curl Malformed-URL Error Means
curl rejects the URL during local parsing; this message is not an HTTP rejection from the server.
A typical diagnostic looks like this:
curl: (3) URL rejected: Malformed input to a URL function
The transfer error is CURLE_URL_MALFORMAT (3): the URL was not properly formatted, as defined in curl’s libcurl error codes.
For a malformed starting URL, curl cannot proceed with that request. Changing server permissions, disabling TLS certificate verification, or retrying the same malformed value will not repair it.
There are two useful qualifications:
- A command can contain multiple URLs. One transfer may succeed before curl rejects another argument.
- A malformed URL can also arise later, such as when an application constructs another request or curl processes a redirect.
Older verbose output may include “Closing connection” after a parsing failure. That line alone does not establish that curl connected to the destination or received a response.
How to Fix the Malformed-URL Error
Shell examples use Bash unless labeled otherwise. Addresses under example.com illustrate URL syntax; replace them with your intended endpoint when testing your application.
Step 1: Inspect the Exact URL curl Receives
- Trace how variables, configuration values, and application defaults combine to form the URL passed to curl. Inspect the finished address, not only its hostname or base URL. A correct base URL can still produce an invalid request if a script appends a value containing a newline or another full URL.
- If your Bash script stores the address in a variable named
url, display its escaped representation:printf '%q\n' "$url"Bash’s
%qformat exposes characters that ordinary terminal output can hide. For example, a carriage return may appear as\r, and an empty value appears as an empty quoted string. - Correct the value at its source. Check for: – An empty or unset variable. – Leading or trailing whitespace. – Embedded carriage returns or newlines. – Literal quote characters inside the address. – Unexpanded configuration placeholders. – A missing hostname or duplicated scheme.
For an HTTP API, make the intended scheme explicit, such as https://. However, curl can infer protocols for some inputs, so the absence of a scheme is not, by itself, proof of this error.
Also distinguish a hostname setting from a base-URL setting. If an application adds the scheme and API path itself, supplying a complete URL where it expects only a hostname can produce the wrong address.
Inspect URLs locally before sharing diagnostics. Query strings and embedded credentials can contain secrets.
Step 2: Quote the URL for Your Shell
- In Bash, enclose a literal URL in straight single quotes:
curl --url 'https://example.com/search?q=curl&lang=en'Without quoting, the shell can interpret characters such as
&rather than passing the complete address to curl. - When using a variable, use double quotes so its contents remain one argument:
curl --url "$url"Single quotes around the variable name would prevent expansion and pass the variable name itself.
- Replace typographic “smart quotes” copied from formatted text with ordinary shell quotes.
Quotes written as shell syntax are normally removed by the shell. Quote characters stored inside a variable or application setting remain part of the data. For example, a hostname containing literal apostrophes can produce an address such as https://'api.example.com'/v1/items.
Shell quoting and URL encoding solve different problems. Quoting preserves an argument; it does not make raw spaces or control characters valid URL data.
Step 3: Encode Path and Query Data Correctly
- Encode spaces and other reserved characters where they belong within a URL component. For a filename containing a space, the path might look like this:
curl --url 'https://example.com/files/monthly%20report.pdf'Do not percent-encode the entire URL as one value. Its scheme separator, path separators, and query delimiters still need to function as URL structure.
- For query parameters, let curl encode the values instead of assembling a query string manually:
curl --get --data-urlencode 'q=hello world' --data-urlencode 'city=New York' --url 'https://example.com/search'Here,
--getplaces the data in the query string, and--data-urlencodeencodes each supplied value. Without--get, these data options normally create a POST request. - If a syntactically usable API URL contains literal brackets or braces, check whether curl’s own URL globbing is interfering. For an endpoint that expects bracketed parameter names, you can disable curl’s globbing:
curl --globoff --url 'https://example.com/search?filter[name]=Ada'This addresses curl’s interpretation of brackets and braces. It does not encode spaces or repair an invalid hostname.
For generated URLs, use your language’s URL-building functions to encode individual components. Avoid repeatedly encoding an already encoded address: that can change the data the server receives.
Step 4: Remove Stray Arguments and Correct Download Paths
- Check the command for stray arguments: curl can treat any argument that is not an option or an option’s value as another URL. That includes a local directory accidentally placed after an option that does not accept a directory.
- If you want the filename from the URL, use
-O/--remote-name. This option does not take a destination directory as its argument. A stray destination path can cause a malformed-URL error even after the intended file downloads successfully (curl mailing-list archive). To save in your Downloads directory, change into it first:cd ~/Downloads/Use the existing directory where you want the file saved. If changing directories fails, correct the path before continuing. Then download using the remote filename:
curl -O --url 'https://curl.se/libcurl/c/libcurl-errors.html'This saves
libcurl-errors.htmlin the current directory. Confirm that the file exists there and contains the expected content. - If you want to specify both the destination directory and filename, use lowercase
-o/--outputinstead:curl --output "$HOME/Downloads/libcurl-errors.html" --url 'https://curl.se/libcurl/c/libcurl-errors.html'The destination directory must exist. Check the named output file after the transfer finishes.
Also check for missing option values. An omitted filename, header value, or other required argument can shift how curl interprets the remainder of the command.
Step 5: Retest the Corrected Request
- For an intended read-only GET request, run the corrected URL with verbose diagnostics:
curl --verbose --url "$url"Here,
urlmust contain the corrected address. You can instead supply your corrected literal URL in quotes. - Confirm that curl proceeds beyond URL parsing and attempts the intended connection. An HTTP response means the request reached an HTTP-speaking service. A DNS, connection, or certificate error means parsing progressed, but a different problem still needs attention.
- Restore any headers, authentication, or other options your request requires, then verify the expected response.
Do not repeatedly replay a state-changing request against production just to test its URL. Use a suitable test environment or a documented read-only endpoint.
Verbose diagnostics can expose sensitive request details. Redact them before sharing.
Fix URL Handling in PowerShell and R
PowerShell
On Windows PowerShell, curl may resolve to an alias rather than the curl executable. Check what will actually run:
Get-Command curl -All
If you intend to use curl for Windows, invoke curl.exe explicitly.
For an existing PowerShell variable named $url, inspect the value using JSON escaping:
$url | ConvertTo-Json -Compress
This makes embedded carriage returns and newlines visible. The surrounding JSON quotes are display formatting, not necessarily characters stored in the URL.
Correct any unwanted characters in the assignment or configuration that supplies the value. Then test the intended read-only request:
curl.exe --verbose --url "$url"
Use straight quotes and avoid copying Bash line-continuation syntax into PowerShell. Keeping a diagnostic command on one line removes that source of confusion.
The repair is complete when the executable receives the intended address and returns the expected response. If curl produces a different error, troubleshoot that specific failure rather than continuing to change URL quoting.
R
In R, inspect the string being passed to the HTTP or download function:
dput(url)
Look for escaped whitespace, embedded quotes, or an incorrectly assembled address. Correct those values where the URL is constructed.
When inserting user-supplied text into a query, encode the component rather than the complete address:
url <- paste0("https://example.com/?q=", URLencode("hello world", reserved = TRUE))
Here, only the query value is encoded; the URL structure remains intact.
To test an HTTP download through R’s libcurl method, choose an output filename that does not already hold a file you need:
download.file(url, destfile = "example.html", method = "libcurl")
Confirm that the download completes and the saved file contains the expected response. If you use an HTTP package instead, inspect the final URL that package constructs; checking only the original input may miss changes made by a wrapper.
Diagnose the Error in libcurl Applications
In an application using libcurl’s easy interface, capture the detailed error message rather than logging only a numeric return code.
Set CURLOPT_ERRORBUFFER on the easy handle before performing the transfer. The buffer can provide a more specific, human-readable explanation of the failure.
For an existing initialized easy handle named handle and a URL string named url, the diagnostic code can look like this:
char error_buffer[CURL_ERROR_SIZE] = {0};
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error_buffer);
curl_easy_setopt(handle, CURLOPT_URL, url);
CURLcode result = curl_easy_perform(handle);
if (result != CURLE_OK) {
fprintf(stderr, "Transfer failed: %s\n",
error_buffer[0]
? error_buffer
: curl_easy_strerror(result));
}
The fallback, curl_easy_strerror, converts a CURLcode into readable diagnostic text when the detailed buffer is empty. Keep the buffer valid while libcurl may use it, and clear or replace the configured buffer before its storage goes out of scope if the handle remains in use.
At the same point in the application, inspect a redacted representation of the final URL supplied to CURLOPT_URL. Logging a hostname earlier in the configuration flow will not reveal malformed characters introduced during later concatenation.
If you are working directly with libcurl’s URL API, its functions return CURLUcode, a separate error-code type. Use curl_url_strerror for those results rather than treating them as easy-interface transfer errors.
After correcting the URL construction, rebuild and rerun the failing operation in an appropriate test environment. Verify both that parsing succeeds and that the request reaches the intended endpoint.
Confirm the Fix and Prevent Another Malformed URL
Before calling the problem solved, check that:
- The destination is correct. A parsable URL can still point to the wrong host or path.
- The response is expected. Receiving a login page or error document is not the same as retrieving the requested resource.
- The original execution environment works. A successful terminal command does not prove that a CI job, container, plugin, or service receives the same configuration.
- The change is in the source of the bad value. Editing a temporary command does not repair a broken environment variable or URL builder.
For prevention, keep schemes, hostnames, paths, and query values separate until you assemble the URL. Validate required configuration before starting the request, and reject unexpected control characters instead of silently deleting them.
Treat signed URLs as opaque values unless you control their generation. Decoding, re-encoding, reordering, or otherwise normalizing them can invalidate their signatures.
Run the corrected request through the script or application that originally failed, then remove temporary diagnostics that expose sensitive data.
Frequently Asked Questions
Is “The Requested URL Was Rejected” the Same Error?
Not necessarily: a browser page saying “The requested URL was rejected” can be an HTTP response from a server, proxy, or security filter. curl’s malformed-input message identifies a URL-parsing failure. Determine whether you received an actual HTTP response before changing server-side access rules.
Does This Error Require Reinstalling curl on Arch Linux?
Usually not; malformed input should be investigated before replacing the package. Inspect the URL and command arguments produced by the failing shell script or application. If the failure began after an update, preserve a minimal reproduction and check whether the caller changed how it constructs or passes the URL.
How Do I Fix This Error in GNS3 or InterSystems?
Find the final URL passed to the failing HTTP operation and compare it with the configuration field’s expected format. A hostname-only field and a full base-URL field are not interchangeable, and invisible characters can enter either one. The error message alone does not identify a particular GNS3 or InterSystems setting, so avoid applying an unrelated product-specific workaround without inspecting that value.
Can I Validate a URL Without Contacting a Server?
Yes: libcurl’s URL API can parse URLs locally without performing an HTTP transfer. Successful parsing does not prove that the hostname resolves, the resource exists, or authentication will succeed. The curl command-line tool normally performs transfers, so it is not a dedicated offline URL validator.
