1

Same curl command is not working with bash script. Could anyone ple

Console try (successful one):

curl -ki \
  --cookie cookie-jar.txt \
  --header 'Content-Type: text/xml' \
  --request POST https://testserver:1234/v1/secondarylogin \
  --data '<?xml version="1.0" encoding="UTF-8"?>
<p:secondaryloginrequest xmlns:p="http://www.ericsson.com/em/am/co/authentication/v1_0/">
    <credential>
        <secret>6673</secret>
        <type>otp</type>
    </credential>
    <channelinformation>COMVIVAAGENTAPP</channelinformation>
</p:secondaryloginrequest>' 

**HTTP/1.1 200 OK**
Date: Wed, 02 Mar 2022 10:52:01 GMT
Content-Type: text/xml;charset=utf-8
Date: Wed, 02 Mar 2022 10:52:02 GMT
Set-Cookie: sessionid=default0c07b834d30644e9b30a9bfced82e6f2
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Length: 0

A very simple Bash Script (failed one):

sh sectest.sh 6673

#!/usr/bin/bash
curl -ki \
  --cookie cookie-jar.txt \
  --header 'Content-Type: text/xml' \
  --request POST https://ugtestpo:8446/v1/secondarylogin \
  --data '<?xml version="1.0" encoding="UTF-8"?>
<p:secondaryloginrequest xmlns:p="http://www.ericsson.com/em/am/co/authentication/v1_0/">
    <credential>
        <secret>"$1"</secret>
        <type>otp</type>
    </credential>
    <channelinformation>COMVIVAAGENTAPP</channelinformation>
</p:secondaryloginrequest>'

HTTP/1.1 401 Unauthorized
Date: Wed, 02 Mar 2022 10:47:53 GMT
Content-Type: text/xml;charset=utf-8
Content-Length: 188

I can not pass my secret value with paramatically.

4
  • 2
    Change secret line with <secret>'"$1"'</secret> Commented Mar 2, 2022 at 11:02
  • Please ignore URLs, they are same in fact. Commented Mar 2, 2022 at 11:02
  • Note also, that in the way you run your script, it is not executed by bash. This doesn't matter for your simple case, but may bite you one day, when you are going to extend it and want to use bash-specific language features. Commented Mar 2, 2022 at 11:32
  • 1
    In addition to the comment by anubhava, I would for the safe side verify that $1 indeed consists only of digits. This would make detecting simple format errors in the script invocation easier. Commented Mar 2, 2022 at 11:34

2 Answers 2

1

Converting my comment to answer so that solution is easy to find for future visitors.

Following should work for you with proper quoting that allows $1 to expand:

curl -ki \
  --cookie cookie-jar.txt \
  --header 'Content-Type: text/xml' \
  --request POST https://ugtestpo:8446/v1/secondarylogin \
  --data '<?xml version="1.0" encoding="UTF-8"?>
<p:secondaryloginrequest xmlns:p="http://www.ericsson.com/em/am/co/authentication/v1_0/">
    <credential>
        <secret>'"$1"'</secret>
        <type>otp</type>
    </credential>
    <channelinformation>COMVIVAAGENTAPP</channelinformation>
</p:secondaryloginrequest>'
Sign up to request clarification or add additional context in comments.

Comments

1

For readability, I'd assign the xml to a variable: heredocs are helpful to avoid quoting hell.

#!/usr/bin/bash

payload=$(cat <<END_XML
<?xml version="1.0" encoding="UTF-8"?>
<p:secondaryloginrequest xmlns:p="http://www.ericsson.com/em/am/co/authentication/v1_0/">
    <credential>
        <secret>$1</secret>
        <type>otp</type>
    </credential>
    <channelinformation>COMVIVAAGENTAPP</channelinformation>
</p:secondaryloginrequest>
END_XML
)

curl -ki \
  --cookie cookie-jar.txt \
  --header 'Content-Type: text/xml' \
  --request POST \
  --data "$payload" \
  https://ugtestpo:8446/v1/secondarylogin

Comments

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.