Troubleshooting of curl errors: Difference between revisions
Tag: wikieditor |
Tag: wikieditor |
||
| (15 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
{{LanguageSwitcher | content = [[Troubleshooting of curl errors | EN]], [[Troubleshooting of curl errors in Mandarin | 漢字]] }} | {{LanguageSwitcher | content = [[Troubleshooting of curl errors | EN]], [[Troubleshooting of curl errors in Mandarin | 漢字]] }} | ||
{{Tips}} How to debug the cUrl command: (1) Enable the [https://ec.haxx.se/usingcurl/verbose verbose] option e.g. "... run the command with the {{kbd | key=<nowiki>-v</nowiki>}} / {{kbd | key=<nowiki>--verbose</nowiki>}} option to get more information." or (2) Enable the [https://ec.haxx.se/usingcurl/verbose/trace trace options] e.g. {{kbd | key=<nowiki>--trace-ascii [filename]</nowiki>}} Note: trace options will be override by verbose option if enable both options. | |||
== How to resolve Command Execution: Works in DOS/Terminal but Not in PHP == | == List of curl errors == | ||
=== How to resolve Command Execution: Works in DOS/Terminal but Not in PHP === | |||
Often, commands that run perfectly when manually executed in DOS or the Windows terminal (like [https://conemu.github.io/ ConEmu]) might not produce the expected outcome when run through PHP. A common issue is the command not generating the anticipated file. | Often, commands that run perfectly when manually executed in DOS or the Windows terminal (like [https://conemu.github.io/ ConEmu]) might not produce the expected outcome when run through PHP. A common issue is the command not generating the anticipated file. | ||
| Line 27: | Line 28: | ||
</pre> | </pre> | ||
== How to resolve cURL Binary Output Warning == | === How to resolve cURL Binary Output Warning === | ||
When executing a cURL command, you might encounter this warning message: | When executing a cURL command, you might encounter this warning message: | ||
| Line 45: | Line 46: | ||
</pre> | </pre> | ||
== How to resolve cURL error: HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) == | === How to resolve cURL error: HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) === | ||
Workaround solution<ref>[https://stackoverflow.com/questions/56865217/php-curl-error-http-2-stream-0-was-not-closed-cleanly-protocol-error-err-1 PHP Curl error HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) - Stack Overflow]</ref>: | Workaround solution<ref>[https://stackoverflow.com/questions/56865217/php-curl-error-http-2-stream-0-was-not-closed-cleanly-protocol-error-err-1 PHP Curl error HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1) - Stack Overflow]</ref>: | ||
<pre> | <pre> | ||
| Line 51: | Line 52: | ||
</pre> | </pre> | ||
== How to resolve cUrl error (3) bad range in URL position xx == | === How to resolve cUrl error (3) bad range in URL position xx === | ||
'''Problem''' | '''Problem''' | ||
| Line 82: | Line 83: | ||
If you're unsure whether encoding is needed, you can use an online URL encoder tool for conversion e.g. [https://www.urlencoder.org/ URL Encode and Decode - Online]. | If you're unsure whether encoding is needed, you can use an online URL encoder tool for conversion e.g. [https://www.urlencoder.org/ URL Encode and Decode - Online]. | ||
== How to resolve cUrl error (#5): Unsupported proxy syntax == | |||
=== How to trace cUrl error (6) Could not resolve host === | |||
This DNS resolution error indicates the hostname doesn't exist. Here are the 3 key debugging steps: | |||
1. Verify the main domain exists | |||
<pre> | |||
curl -i https://example.com | |||
</pre> | |||
2. Check subdomain DNS resolution | |||
<pre> | |||
nslookup subdomain.example.com | |||
</pre> | |||
3. Test correct URL structure | |||
<pre> | |||
# Maybe it's a path instead of subdomain | |||
curl -i "https://www.example.com/subdomain/..." | |||
# Or try common subdomain alternatives | |||
nslookup www.example.com | |||
nslookup api.example.com | |||
</pre> | |||
Conclusion: If you get {{kbd | key=NXDOMAIN}}, the subdomain doesn't exist - verify the correct URL from your source. | |||
=== How to resolve cUrl error (#5): Unsupported proxy syntax === | |||
Condition: the username of [https://en.wikipedia.org/wiki/SOCKS socks5 proxy] contains @ symbol such as {{kbd | key=<nowiki>[email protected]</nowiki>}} | Condition: the username of [https://en.wikipedia.org/wiki/SOCKS socks5 proxy] contains @ symbol such as {{kbd | key=<nowiki>[email protected]</nowiki>}} | ||
<pre> | <pre> | ||
| Line 98: | Line 126: | ||
</pre> | </pre> | ||
== How to resolve cUrl error (#56): OpenSSL SSL_read: No error. == | === How to resolve cUrl error (#56): OpenSSL SSL_read: No error. === | ||
Condition: The curl vesion 7.67.0 on windows met problem. Result of [https://www.php.net/manual/en/function.curl-version.php curl_version] returned: | Condition: The curl vesion 7.67.0 on windows met problem. Result of [https://www.php.net/manual/en/function.curl-version.php curl_version] returned: | ||
<pre> | <pre> | ||
| Line 119: | Line 147: | ||
* [https://github.com/curl/curl/issues/4982 cURL error 56: OpenSSL SSL_read: No error information · Issue #4982 · curl/curl] | * [https://github.com/curl/curl/issues/4982 cURL error 56: OpenSSL SSL_read: No error information · Issue #4982 · curl/curl] | ||
== How to resolve cURL error: SSL: no alternative certificate subject name matches target host name xxx == | === How to resolve cURL error: SSL: no alternative certificate subject name matches target host name xxx === | ||
Walkaround solution<ref>[https://stackoverflow.com/questions/20842970/curl-fix-curl-51-ssl-error-no-alternative-certificate-subject-name-matches Curl: Fix CURL (51) SSL error: no alternative certificate subject name matches - Stack Overflow]</ref>: | Walkaround solution<ref>[https://stackoverflow.com/questions/20842970/curl-fix-curl-51-ssl-error-no-alternative-certificate-subject-name-matches Curl: Fix CURL (51) SSL error: no alternative certificate subject name matches - Stack Overflow]</ref>: | ||
| Line 127: | Line 155: | ||
</pre> | </pre> | ||
== How to resolve the error: error code: 1010 == | === How to resolve the error: error code: 1010 === | ||
Identifying the Issue | Identifying the Issue | ||
| Line 141: | Line 169: | ||
* Instead of relying on the cURL command, switching to a JavaScript-based crawler is recommended as a solution to this problem <ref>[https://www.zenrows.com/blog/cloudflare-error-1010#how-to-avoid Cloudflare Error 1010: What Is It and How to Avoid - ZenRows]</ref>. | * Instead of relying on the cURL command, switching to a JavaScript-based crawler is recommended as a solution to this problem <ref>[https://www.zenrows.com/blog/cloudflare-error-1010#how-to-avoid Cloudflare Error 1010: What Is It and How to Avoid - ZenRows]</ref>. | ||
== How to resolve Error 405 Method Not Allowed == | === How to resolve cURL Bad Range in URL Error === | ||
'''Problem Analysis:''' | |||
When executing a cURL command with square brackets in the URL, you might encounter this error message: | |||
<pre> | |||
curl: (3) bad range in URL position 38: | |||
https://example.com/api/news?q[post_category_id_eq]=&tag_id=176&page=2 | |||
</pre> | |||
'''Solutions''' | |||
To resolve this error, append the {{kbd | key=<nowiki>--globoff</nowiki>}} flag to your cURL command: | |||
Example: | |||
<pre> | |||
curl --globoff "https://example.com/api/news?q[post_category_id_eq]=&tag_id=176&page=2" -o output.html | |||
</pre> | |||
'''What does --globoff do?''' | |||
The {{kbd | key=<nowiki>--globoff</nowiki>}} (or {{kbd | key=<nowiki>-g</nowiki>}}) parameter disables cURL's URL globbing feature. | |||
By default, cURL interprets certain characters as wildcards for batch downloading: | |||
* Square brackets {{kbd | key=<nowiki>[]</nowiki>}} - Character ranges or lists | |||
* Curly braces {{kbd | key=<nowiki>{}</nowiki>}} - String sequences | |||
When your URL contains these characters as literal query parameters (common in Rails and similar frameworks), cURL misinterprets them as globbing patterns. The {{kbd | key=<nowiki>--globoff</nowiki>}} flag tells cURL to treat all characters literally instead of parsing them as wildcards. | |||
Alternative solutions: | |||
* URL-encode the brackets: {{kbd | key=<nowiki>%5B</nowiki>}} for {{kbd | key=<nowiki>[</nowiki>}} and {{kbd | key=<nowiki>%5D</nowiki>}} for {{kbd | key=<nowiki>]</nowiki>}} | |||
=== How to resolve 400 Bad Request Errors: UTF-8 Character Encoding Issues === | |||
'''Problem Analysis:''' | |||
Example cURL command for macOS terminal with error: | |||
<pre> | |||
$ curl "https://api.search.brave.com/res/v1/web/search?q=\"XXX\"+公司簡介+產品+服務" \ | |||
-H "Accept: application/json" \ | |||
-H "Accept-Encoding: gzip" \ | |||
-H "X-Subscription-Token: YOUR_API_KEY_HERE" | |||
> <html><body><h1>400 Bad request</h1> | |||
Your browser sent an invalid request. | |||
</body></html> | |||
</pre> | |||
'''Solutions''' | |||
Properly Handle URL Encoding: Chinese characters and special symbols need to be correctly URL-encoded to avoid 400 Bad Request errors (online tool: [https://www.urlencoder.org/ URL Encode and Decode - Online]): | |||
* {{kbd | key=<nowiki>"XXX"</nowiki>}} → {{kbd | key=<nowiki>%22XXX%22</nowiki>}} | |||
* {{kbd | key=<nowiki>公司簡介</nowiki>}} → {{kbd | key=<nowiki>%E5%85%AC%E5%8F%B8%E7%B0%A1%E4%BB%8B</nowiki>}} | |||
* {{kbd | key=<nowiki>產品</nowiki>}} → {{kbd | key=<nowiki>%E7%94%A2%E5%93%81</nowiki>}} | |||
* {{kbd | key=<nowiki>服務</nowiki>}} → {{kbd | key=<nowiki>%E6%9C%8D%E5%8B%99</nowiki>}} | |||
'''Revised cURL Command:''' | |||
<pre> | |||
$ curl "https://api.search.brave.com/res/v1/web/search?q=%22XXX%22+%E5%85%AC%E5%8F%B8%E7%B0%A1%E4%BB%8B+%E7%94%A2%E5%93%81+%E6%9C%8D%E5%8B%99" \ | |||
-H "Accept: application/json" \ | |||
-H "Accept-Encoding: gzip" \ | |||
-H "X-Subscription-Token: YOUR_API_KEY_HERE" | |||
</pre> | |||
=== How to resolve Error 405 Method Not Allowed === | |||
When I tried to crawl the web resource but met the error message: "Error 405 Method Not Allowed" | When I tried to crawl the web resource but met the error message: "Error 405 Method Not Allowed" | ||
Solution: Make sure using the correct HTTP request method<ref>[https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html HTTP/1.1: Method Definitions]</ref> e.g. {{kbd | key=GET}} or {{kbd | key=POST}} to access the web resource. | Solution: Make sure using the correct HTTP request method<ref>[https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html HTTP/1.1: Method Definitions]</ref> e.g. {{kbd | key=GET}} or {{kbd | key=POST}} to access the web resource. | ||
== How to resolve Error 415 Unsupported Media Type == | === How to resolve Error 415 Unsupported Media Type === | ||
When I tried to POST JSON (as payload) but met the error message: "Error 415 Unsupported Media Type" | When I tried to POST JSON (as payload) but met the error message: "Error 415 Unsupported Media Type" | ||
| Line 155: | Line 247: | ||
</pre> | </pre> | ||
== How to rseolve cURL returns binary data instead of html or json == | === How to rseolve cURL returns binary data instead of html or json === | ||
* [https://stackoverflow.com/questions/28283822/curl-returns-binary-data-instead-of-html php - cURL returns binary data instead of html - Stack Overflow] | * [https://stackoverflow.com/questions/28283822/curl-returns-binary-data-instead-of-html php - cURL returns binary data instead of html - Stack Overflow] | ||
* [https://errerrors.blogspot.com/2020/06/resolve-curl-returns-binary-data-instead-of-html-or-json.html 解決 cUrl 下載的網頁或 JSON 看起來像是亂碼的問題] | * [https://errerrors.blogspot.com/2020/06/resolve-curl-returns-binary-data-instead-of-html-or-json.html 解決 cUrl 下載的網頁或 JSON 看起來像是亂碼的問題] | ||
== How to resolve the downloaded file was empty == | === How to resolve the downloaded file was empty === | ||
Error condition | Error condition | ||
* When I executed the Curl command {{kbd | key=<nowiki>curl -O https://path/to/plain_text.txt</nowiki>}}, but the downloaded file was empty. | * When I executed the Curl command {{kbd | key=<nowiki>curl -O https://path/to/plain_text.txt</nowiki>}}, but the downloaded file was empty. | ||
| Line 180: | Line 272: | ||
</pre> | </pre> | ||
== How to resolve the error: no matches found == | === How to resolve the error: no matches found === | ||
'''Error condition''' | '''Error condition''' | ||
* When I executed the Curl command {{kbd | key=<nowiki>curl http://website.test/http_status.php?case=500</nowiki>}}, but I met the error message: "zsh: no matches found: <nowiki>http://website.test/http_status.php?case=500</nowiki>" | * When I executed the Curl command {{kbd | key=<nowiki>curl http://website.test/http_status.php?case=500</nowiki>}}, but I met the error message: "zsh: no matches found: <nowiki>http://website.test/http_status.php?case=500</nowiki>" | ||
| Line 188: | Line 280: | ||
'''Explanation''' by Claude | '''Explanation''' by Claude | ||
<div class="toccolours mw-collapsible mw-collapsed" style="width:400px; overflow:auto;"> | |||
When you see a "no matches found" error in zsh (a type of shell), it's because the shell gets confused by special characters in your command. The main problem is the question mark (?) in the URL. | When you see a "no matches found" error in zsh (a type of shell), it's because the shell gets confused by special characters in your command. The main problem is the question mark (?) in the URL. | ||
| Line 204: | Line 296: | ||
The fix is simple: put double quotes ("") around your URL. This tells the shell "just use these characters exactly as they are - don't try to do anything special with them." Now the ? goes straight to curl as part of the web address, instead of being used as a special character by the shell. | The fix is simple: put double quotes ("") around your URL. This tells the shell "just use these characters exactly as they are - don't try to do anything special with them." Now the ? goes straight to curl as part of the web address, instead of being used as a special character by the shell. | ||
== How to resolve the error: Remote file name has no length == | </div> | ||
=== How to resolve the error: Remote file name has no length === | |||
Error message | Error message | ||
| Line 221: | Line 315: | ||
* When you encounter an error while downloading a file with `curl`, it may be because the URL does not include a file name, causing `curl` to be unable to determine the name to save the file under. The solution is to manually specify a file name using the `-o` option, for example, `curl -o myfile.html "https://website.com/?p=123"`. Additionally, make sure you have enough disk space and are in a directory where you have write permissions. | * When you encounter an error while downloading a file with `curl`, it may be because the URL does not include a file name, causing `curl` to be unable to determine the name to save the file under. The solution is to manually specify a file name using the `-o` option, for example, `curl -o myfile.html "https://website.com/?p=123"`. Additionally, make sure you have enough disk space and are in a directory where you have write permissions. | ||
== How to resolve | === How to resolve zsh parse error near `&' === | ||
Error condition | Error condition | ||
<pre> | <pre> | ||
| Line 239: | Line 333: | ||
</pre> | </pre> | ||
== How to Suppress the Progress Meter == | === How to resolve zsh Event Not Found Error === | ||
When executing a cURL command with JSON data in zsh shell, you might encounter this error message: | |||
<pre> | |||
> zsh: event not found: \ | |||
</pre> | |||
This occurs because zsh interprets the exclamation mark (!) as a history expansion character, even when it appears inside double quotes. | |||
To resolve this error, use one of these approaches: | |||
'''Method 1: Use single quotes for JSON data''' | |||
{{kbd | key=<nowiki>-d '{"message": "Hello '\''world'\'' & welcome!", "price": "$19.99"}'</nowiki>}} | |||
'''Method 2: Escape the exclamation mark''' | |||
{{kbd | key=<nowiki>-d "{\"message\": \"Hello 'world' & welcome\!\", \"price\": \"\$19.99\"}"</nowiki>}} | |||
'''Method 3: Use heredoc with stdin''' | |||
{{kbd | key=<nowiki>-d @- << 'EOF'</nowiki>}} | |||
'''Method 4: Save JSON to file and reference it''' | |||
{{kbd | key=<nowiki>-d @payload.json</nowiki>}} | |||
'''Method 5: Disable history expansion temporarily'''<ref>[https://www.reddit.com/r/bash/comments/cl54to/not_sure_why_mac_is_saying_bash_event_not_found/?show=original not sure why mac is saying `-bash: !": event not found` when I try to simply echo a string containing a "!" : r/bash]</ref> | |||
{{kbd | key=<nowiki>set +H</nowiki>}} (before the command) | |||
Example with corrected syntax: | |||
Using single quotes: | |||
<pre> | |||
curl -X POST \ | |||
-H "Content-Type: application/json" \ | |||
-d '{"message": "Hello '\''world'\'' & welcome!", "price": "$19.99"}' \ | |||
-v \ | |||
https://example.com/api/ | |||
</pre> | |||
Using heredoc (recommended for complex JSON): | |||
<pre> | |||
curl -X POST \ | |||
-H "Content-Type: application/json" \ | |||
-d @- \ | |||
-v \ | |||
https://example.com/api/ << 'EOF' | |||
{ | |||
"message": "Hello 'world' & welcome!", | |||
"price": "$19.99", | |||
"special_chars": "!@#$%^&*()_+{}|:<>?[]\\;'\",./" | |||
} | |||
EOF | |||
</pre> | |||
Using external JSON file: | |||
<pre> | |||
curl -X POST \ | |||
-H "Content-Type: application/json" \ | |||
-d @payload.json \ | |||
-v \ | |||
https://example.com/api/ | |||
</pre> | |||
Note: The heredoc approach is recommended for complex JSON as it avoids all shell interpretation issues and maintains readable formatting. | |||
=== How to Suppress the Progress Meter === | |||
When executing the curl command, the typical output includes detailed information about the download and upload process as shown below: | When executing the curl command, the typical output includes detailed information about the download and upload process as shown below: | ||
<pre> | <pre> | ||
% curl -o sample.pdf https://example.com/sample.pdf | |||
% Total % Received % Xferd Average Speed Time Time Time Current | % Total % Received % Xferd Average Speed Time Time Time Current | ||
Dload Upload Total Spent Left Speed | Dload Upload Total Spent Left Speed | ||
| Line 249: | Line 408: | ||
To suppress the progress meter of the curl command, you can use the {{kbd | key=<nowiki>-s</nowiki>}} (silent) option, which hides all output, or the {{kbd | key=<nowiki>-S</nowiki>}} (silent with error messages) option, which suppresses the progress meter but still displays error messages. | To suppress the progress meter of the curl command, you can use the {{kbd | key=<nowiki>-s</nowiki>}} (silent) option, which hides all output, or the {{kbd | key=<nowiki>-S</nowiki>}} (silent with error messages) option, which suppresses the progress meter but still displays error messages. | ||
<pre> | <pre> | ||
curl - | curl -sSo file.pdf [URL] | ||
</pre> | </pre> | ||
Alternatively, if you prefer to suppress all output including error messages, use the {{kbd | key=<nowiki>-s</nowiki>}} (silent) option alone: | Alternatively, if you prefer to suppress all output including error messages, use the {{kbd | key=<nowiki>-s</nowiki>}} (silent) option alone: | ||
<pre> | <pre> | ||
curl - | curl -so file.pdf [URL] | ||
</pre> | </pre> | ||
== cURL JSON Payload Issues in Windows vs Mac/Linux Systems == | Suggestion: | ||
<pre> | |||
curl -sSfo file.pdf [URL] | |||
</pre> | |||
Adding {{kbd | key=<nowiki>-f</nowiki>}} causes curl to fail on HTTP errors (4xx, 5xx), making it the most robust option for scripts. | |||
<table class="wikitable sortable"> | |||
<tr> | |||
<th>Flag</th> | |||
<th>Error type caught</th> | |||
</tr> | |||
<tr> | |||
<td><span class="flag">-S</span></td> | |||
<td>Network-level errors (DNS failure, connection refused)</td> | |||
</tr> | |||
<tr> | |||
<td><span class="flag">-f</span></td> | |||
<td>HTTP-level errors (4xx, 5xx)</td> | |||
</tr> | |||
<tr> | |||
<td><span class="flag">-sS</span></td> | |||
<td>Silent + network errors</td> | |||
</tr> | |||
<tr> | |||
<td><span class="flag">-sSf</span></td> | |||
<td>Silent + network errors + HTTP errors</td> | |||
</tr> | |||
</table> | |||
=== How to resolve cURL Illegal Filename Character Error === | |||
When executing a cURL command with illegal characters in the output filename, you might encounter this error message: | |||
<pre> | |||
curl -o $'example\ntest.html' http://example.com | |||
Warning: Failed to open the file $'example\ntest.html': No such file or | |||
Warning: directory | |||
curl: (23) client returned ERROR on write of 1256 bytes | |||
</pre> | |||
To resolve this error, use valid filename characters in your {{kbd | key=<nowiki>-o</nowiki>}} or {{kbd | key=<nowiki>--output</nowiki>}} flag: | |||
* Remove illegal characters like: {{kbd | key=<nowiki>< > : " | ? * \</nowiki>}} and control characters | |||
* Avoid path separators: {{kbd | key=<nowiki>/</nowiki>}} (unless creating directories) | |||
* Use safe characters: {{kbd | key=<nowiki>a-z A-Z 0-9 - _ .</nowiki>}} | |||
* Create directories first if needed: {{kbd | key=<nowiki>mkdir -p directory</nowiki>}} | |||
=== cURL JSON Payload Issues in Windows vs Mac/Linux Systems === | |||
'''1. Problem''' | '''1. Problem''' | ||
| Line 291: | Line 496: | ||
* Mac/Linux: Can use single quotes to wrap the complete JSON string | * Mac/Linux: Can use single quotes to wrap the complete JSON string | ||
* When using incorrect quote formatting, the JSON payload is parsed incorrectly, causing the API to return a format error message | * When using incorrect quote formatting, the JSON payload is parsed incorrectly, causing the API to return a format error message | ||
== Related security issue == | |||
* [https://owasp.org/www-community/attacks/Command_Injection Command Injection] "an attack technique in which an attacker exploits vulnerabilities in an application to inject and execute malicious operating system commands, typically through unvalidated user input such as forms, cookies, or HTTP headers." | |||
== Further reading == | == Further reading == | ||
| Line 301: | Line 509: | ||
<references /> | <references /> | ||
{{Template:Troubleshooting}} | {{Template: Troubleshooting}} | ||
[[Category: Web server]] [[Category: Programming]] [[Category: Web scraping]] [[Category: Revised with LLMs]] | [[Category: Web server]] [[Category: Programming]] [[Category: Web scraping]] [[Category: Revised with LLMs]] | ||