cURL not returning error code

February 4, 2011 at 11:46:45
Specs: Windows 7
I am using cURL in a script to grab files at set intervals. My script currently includes the following:

"C:\cURL\curl.exe" -u username:password -R --remote-name-all -K "C:\curl\filelist.txt"
if not ERRORLEVEL 0 goto end

After this line I call an asp page that deletes the files that I just downloaded. Normally this works without issue but occasionally the server will return a 500 internal server error. When this happens, cURL still responds with an ERRORLEVEL of 0 instead of something else identifying a problem. So I am sometimes deleting files before I download them. The file names are dynamic so its not an option to check to see if a certain file exists. Any other thoughts?

This specific script runs on various versions of Windows workstation and server all with the same results.

See More: cURL not returning error code

Report •

February 4, 2011 at 11:52:18
if not ERRORLEVEL 0 goto end
This translates into IF ERRORLEVEL < 0 GOTO END, and so this test is always false, and the GOTO will never be reached. This is independent of whatever cURL's return value policy, which I haven't looked up.

How To Ask Questions The Smart Way

Report •

February 4, 2011 at 12:14:15
No thats not true. What you mention is completely different

IF ERRORLEVEL < 0 GOTO END is not at all the same thing as if not ERRORLEVEL 0 goto end. In my case I want it to continue processing if ERRORLEVEL is 0 which means status of OK. In your example you are saying is that if it is < go to END, completely different statement.

My statement means if the ERRORLEVEL is 0 continue, otherwise go to end which is the desired behavior and what is actually happening. If there is an error in download of the files it goes to end as ERRORLEVEL in that case is not 0. I direct this to go to end if there is an error as I dont want to proceed with calling the asp and deleting the files if there is an error. The problem is when the server returns a status of 500 for some reason cURL is not relaying that properly and is returning an errorlevel 0 which translates to everything is ok even though the files were never downloaded.

Report •

February 4, 2011 at 12:17:12
I should also mention that with my logic if there is a 404 returned, I do get an ERRORLEVEL of 1 and the script properly goes to end as it should. If there is a server reset, etc that prevents the complete file from downloading then it also goes to end as it should. The 500 Internal server error is the only error that cURL seems to pass through as ERRORLEVEL 0 for some reason.

Report •

Related Solutions

February 4, 2011 at 13:47:53
Required reading:
IF /?

ERRORLEVEL number Specifies a true condition if the last program run
                  returned an exit code equal to or greater than the number

How To Ask Questions The Smart Way

Report •

February 4, 2011 at 14:06:40
If I was saying IF ERRORLEVEL 0 then you would have a point, but I was saying "IF NOT ERRORLEVEL 0" which makes my statement completely different than what you are saying. Do the same help yourself for IF /? and you will see when NOT is used the statement is handled differently. Still unsure how this conditions works, try writing some basic scripts yourself using this logic and you will see it works as I am explaining.

Again the issue here is not the logic which I am positive works as designed. I am using this logic is several production scripts with the expected results and is behaving correctly based on the error code returned. The issue is that cURL is returning error code 0 upon an HTTP 500 error. I was looking to see if someone had other suggestions on how to get around this such as parsing the results returned from cURL using -v for example.

Report •

February 4, 2011 at 14:47:18
1) IF ERRORLEVEL is equivalent to IF ERRORLEVEL >=
2) NOT >= is equivalent to <
3) IF ERRORLEVEL 0 always fires*
4) IF NOT ERRORLEVEL 0 never fires*
5) IF ERRORLEVEL 1 fires if the exit code isn't 0.
6) IF NOT %ERRORLEVEL%==0 will only fire if ERRORLEVEL is non-zero, and seems to follow your line of logic, but isn't what you posted.

*) Technically, Win32 allows negative return codes. In practice, I have yet to see a program return anything other than 0 or a positive value, but there you go.

I'm not sure how to say it. Maybe raw output?

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\>exit 3

C:\>if not errorlevel 0 @echo Y

C:\>if errorlevel 1 @echo Y

C:\>echo %errorlevel%


How To Ask Questions The Smart Way

Report •

February 4, 2011 at 15:12:34

You are completely correct here, I am doing too many things at once and my fault for not doing a copy/paste. What I am using is:


That said... any thoughts on a way around the HTTP 500 error code that cURL is returning as 0?

Report •

February 4, 2011 at 15:18:58
Edited post above as you are correct, in the syntax. Doing too many things at once here.

Report •

February 4, 2011 at 18:52:50
Rozer's Logic is correct ,this is how commad processor behaves..

Where you are saving the files which are being downloaded by your exe? and how you get the download file name ?

If the download link (Filename) is coming from C:\curl\filelist.txt" is below possible with Curl.exe ?
1. use a for loop which will take one line from C:\curl\filelist.txt at a time
2 It will track the target filename (If the download file is coming from that text file then it should possible.
3 Curl.exe will get executed , this time it will get single line inputs using loop not from the actual text file.does curl.exe have the option to get single line input instead of filelist ?
4) once curl.exe finished executation,the loop will check for the downloaded file. It should have the file name from step 2.
5) check using if exist command whether the file is present or not and then proceed accordingly.

If it works I guess it will be more reliable as it wont depend on curl error code.

What i mean here is

Subhash Chandra.

Report •

February 5, 2011 at 08:03:28
Well, the easiest way would be to see if the files you're trying to grab exist. If not, obviously something failed.

The other option is to dump the STDOUT and/or STDERR to a file, then parse that for errors.

How To Ask Questions The Smart Way

Report •

February 5, 2011 at 12:31:20
You might want to try the -f flag of curl:

              (HTTP) Fail silently (no output at all) on server  errors.  This
              is  mostly done to better enable scripts etc to better deal with
              failed attempts. In normal cases when a  HTTP  server  fails  to
              deliver  a  document,  it  returns  an  HTML document stating so
              (which often also describes why and more). This flag  will  pre-
              vent curl from outputting that and return error 22.

              This  method is not fail-safe and there are occasions where non-
              successful response codes will  slip  through,  especially  when
              authentication is involved (response codes 401 and 407).

However, I think that this may not work when you are trying to get files from a list. What if your list contains 100 URLs, and you get a server error in one of them but the other 99 are ok? If you use a loop (using FOR /F, try typing FOR /? for help) to read the file list, you can then check for an error each time.

Report •

February 7, 2011 at 08:10:05
The suggestion about looping the file list and checking for each of them is good in theory but in my case, I may download a changed file and the original one is still in the same location.(, and then download again so checking for file names again wouldnt necessarily give accurate results. Also, there are cases on other servers where the file is downloaded to a temp directory and moved almost immediately so that would show a false positive.

To answer the question of how it works now... if there is a list of files downloaded and one gives and error then it will fail and goto end (assuming currently it is not giving a 500 error) but if another error is reported in the list it does properly fail.

using -f/--fail the reason I hadnt used that is that we used authentication on our servers and curl man pages mentions

"This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407)."

It is still something to try though so I will do some testing with --fail and see how it works out.

Report •

February 7, 2011 at 08:24:44
Looks like I was already using -f for the download of the filelist.txt but I wasnt using -f in the command where the files are actually downloaded from the filelist.txt. I have added it to that second line now as well so will see how it works out.

Report •

Ask Question