Timeline for Getting curl to output HTTP status code?
Current License: CC BY-SA 4.0
12 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 24, 2023 at 21:57 | comment | added | Bruce |
Once the output is redirected to &3 it's unbuffered and gone so you can't redirect it further. Try this to get both. By sending the output back to stdout (&1) using process substitution, you can reorder things so the response code is followed by the output. This makes it easy to read the status and body into variables because read gobbles up everything after the status line and stuffs it into the "body" variable. IFS=$'\n' read -r -d '' http_status body < <(curl -s -w "%{http_code}\n" -o >(IFS= read -r -d '' -u0 stdin; printf "%s" "$stdin") $URL)
|
|
Jan 4, 2022 at 22:52 | comment | added | TMB | If you want it in a variable, but don't want the whole response body to STDOUT, you can do this. HTTP_STATUS=$(curl -s -w "%{http_code}" -o /dev/null 'example.org') | |
Dec 1, 2019 at 6:15 | comment | added | Tuntable | Good, bug fails in cygwin, Warning: Failed to create the file /dev/fd/63: No such file or directory. Use -o curl_res.txt | |
May 31, 2019 at 7:01 | comment | added | martsraits |
Is it necessary to close the file descriptor (exec 3>&- ) after curl has finished?
|
|
Apr 15, 2019 at 15:29 | history | edited | Heath Borders | CC BY-SA 4.0 |
Space after comment start for consistency
|
Feb 8, 2019 at 9:40 | comment | added | akostadinov | good answer, you can also redirect to a real file and cat it later if you want portability of shells | |
Oct 11, 2018 at 19:42 | history | edited | Heath Borders | CC BY-SA 4.0 |
Updated to note which shell this was written for
|
Oct 11, 2018 at 14:47 | comment | added | SamK | Does not work with /bin/sh. | |
Jul 21, 2015 at 3:10 | comment | added | Heath Borders |
The output is in STDOUT , so you should be able to redirect output from the command to anywhere you like just like a regular command. I haven't tested this though.
|
|
Mar 12, 2015 at 1:46 | comment | added | Roger Filmyer | Now how, in turn, can I redirect the output to another variable? | |
Jan 30, 2015 at 21:14 | comment | added | spyle | That's serious slickery...and I like it! | |
Jan 8, 2015 at 20:59 | history | answered | Heath Borders | CC BY-SA 3.0 |