3

so I am new to curl and am trying to write a bash script that will I can run that downloads a file. So I start off by authentication then make a POST to request a download. I am given a Foo_ID in which I parse using bash and set to a parameter. I then try to use GET the certain Foo data via a download URL. The issue I am having is that whenever I pass in the parameter I parsed from the POST response I get nothing. Here is an example of what I am doing.

#!/bin/bash
curl -b cookies -c cookies -X POST @login_info -d "https://foo.bar.com/auth"
curl -b cookies -c cookies -X POST @Foo_info -d "https://foo.bar.com/foos" > ./tmp/stuff.t
myFooID=`cat ./tmp/stuff.t |grep -Po '"foo_id:.*?",'|cut -d '"' -f 4`
curl -b cookies -c cookies "http://foo.bar.com/foo-download?id=${myFooID}" > ./myFoos/Foo1.data

I have echo'd myFooID to make sure it is correct and it is. I have also echo'd "https://foo.bar.com/foo-download?id=${myFooID}" and it is properly showing the URL I need. Could anyone help me with this like I said I am new to using curl and a little rusty on using bash commands.

4
  • the way you use the parameter seems correct, can you also echo the result of the curl command? Commented May 31, 2012 at 23:03
  • When I echo' the result of the last curl command It is just blank. Commented May 31, 2012 at 23:41
  • Make sure you quote the variable when you echo it. Do echo "$myFooID" and see if it looks OK. Then do echo -n "$myFooID" | hexdump -C and see if the hex values show any unexpected characters. If you issue the commands by hand, but substitute the ID literally instead of with a variable, does it work? You don't need cat, grep accepts filenames as arguments. You should be able to use a variable instead of a temporary file. Use $() instead of backticks. These suggestions won't solve your problem though. Commented May 31, 2012 at 23:43
  • Thank you Dennis! I cleaned up my code with your suggestions and now it works perfectly! You don't know how long I have been trying to solve this. Commented May 31, 2012 at 23:52

1 Answer 1

2

So I have solved the issue. The problem was after doing my post for the Foo I didn't give enough time for my foo to be created before trying to download it. I added a sleep command between both the last two curl commands and now it works perfectly. I would like to thank Dennis Williamson for helping me clean up my code wich led me to understanding my issue. I have created a shrine for him on my desk.

Sign up to request clarification or add additional context in comments.

1 Comment

For anyone looking to do the same in the -d post data: stackoverflow.com/questions/4152388/expanding-variable-in-curl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.