Get http code when uploading file using curl and printing curl output

Viewed 255

I'm writing a bash script where I want to upload a file using curl. I want to print in a log file the output from stdout and stderr (to see if the upload is being completed). This is what works so far:

curl -T home/user/mydir/filename <my url>/.../filename >> home/user/mydir/script.log 2>&1

Now, I also want to get the returned http code so I can use it somewhere else in my script. The following works as well:

CODE=$(curl -w '%{http_code}' -T home/user/mydir/filename <my url>/.../filename)

How can I combine these two commands? I want to print the stdout and stderr in the log file (append, to be precise) and also store the returned http code in a variable for later use.

Thank you.

1 Answers

Can you do something like this (the first < line is the return code) (from the verbose option). The -o can be - (stdout) OR any other file you want??

# curl -v -o /dev/null http://www.google.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 142.250.76.100...
* TCP_NODELAY set
* Connected to www.google.com (142.250.76.100) port 80 (#0)
> GET / HTTP/1.1
> Host: www.google.com
> User-Agent: curl/7.64.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 10 Mar 2021 02:37:46 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
< Server: gws
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< Set-Cookie: 1P_JAR=2021-03-10-02; expires=Fri, 09-Apr-2021 02:37:46 GMT; path=/; domain=.google.com; Secure
< Set-Cookie: NID=211=l3bDHkrWaIiQN03V4QI2bXrwHu0ZTMxoQgB1DDhIE2EfomQO6zrAPbu-5h6L6Ru60kQh0vtAog3iykbLvvtv28r8aVYiyMapXQWTexMArSIqRVQrcwDL4PnaoivdpE1_aL0rC6gohDpxFT-yQmk3jkE7EKtdSL_Dh7Z7nsnoqBM; expires=Thu, 09-Sep-2021 02:37:46 GMT; path=/; domain=.google.com; HttpOnly
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
< 
{ [686 bytes data]
100 13716    0 13716    0     0  37889      0 --:--:-- --:--:-- --:--:-- 37889
* Connection #0 to host www.google.com left intact
* Closing connection 0
Related