0

I have trouble getting curl to return a response. When I comment out the while loop or when I pass in a hardcoded URL, curl works perfectly. As you can see in the code below, curl does not work and displays an error "curl: (3) URL using bad/illegal format or missing URL". I suspect that "${REQUEST_STR}" is the problem. Does anyone know a correct way to pass a string variable to curl? Thank you so much.

while read -r POS_SN
do
    # echo "${POS_SN}"
    REQUEST_STR="${BASE_URL}?usercode=${USER_CODE}&userpass=${USER_PASS}&sn=${POS_SN}"

    echo "Request: ${REQUEST_STR}"
    echo "Response: "
    curl  "${REQUEST_STR}"

done < "$input"

SOLUTION: I added IFS=$'\r' to the line while IFS=$\'r' read -r POS_SN and now curl works perfectly. I'm still not sure why it works but it works :)

Toto
  • 89,455
  • 62
  • 89
  • 125
notClickBait
  • 101
  • 1
  • 1
  • 12
  • Are your BASE_URL, USER_CODE, and USER_PASS variables set properly? You can also try executing your script via bash -x for more debug information. https://www.shellcheck.net/ is also a great tool for debugging common bash issues. – j_b Aug 24 '21 at 19:12
  • Check your file for carriage returns – that other guy Aug 24 '21 at 19:20
  • You have an `echo "Request: ${REQUEST_STR}"`. Did you try ` curl "${REQUEST_STR}"` from the commandline? – Walter A Aug 24 '21 at 20:22
  • @notClickBait: From the error message, I wuld conclude that `BASE_URL` is not set properly. Did you try to run the code with ´-x`? – user1934428 Aug 25 '21 at 07:20

1 Answers1

1

Something like the following should probably work for you assuming you set the base_url, user_code, and user_pass variables according to your needs:

#!/bin/bash

base_url="https://www.google.com"
user_code="some_user_code"
user_pass="some_user_pass"


while read -r pos_sn
do
    echo "${pos_sn}"
    url="${base_url}?usercode=${user_code}&userpass=${user_pass}&sn=${pos_sn}"

    echo "Request: ${url}"
    echo "Response: "
    curl "${url}"

done
j_b
  • 1,975
  • 3
  • 8
  • 14