LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-05-2019, 08:10 PM   #1
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
Variable Expansion REST Api Calls


Hello all,

I am on mac using bash 3.2 ...I am writing a curl command to communicate with a provider API but having some issue. My problem is I want to variablize the command but when I substitute double quotes for single quotes, it does not work

below works, but of course I cannot perform variable expansion due to the single quotes. I have tried alot based on google research, but nothing works, can anyone assist.

Code:
curl -X POST 'https://api
.json' \
     -H 'X-Api-Key:rr' -i \
     -H 'Content-Type: application/json' \
     -d \
'{
  "deployment": {
    "revision": "${VER}",
    "changelog": "${TEMP}",
    "description": "string",
    "user": "${USER}"
  }
}'
below doesn't work I get internal server error
Code:
curl -X POST "https://api..json" \
     -H "X-Api-Key:43439f" -i \
     -H "Content-Type: application/json" \
     -d \
"{
  "deployment": {
    "revision": "${VER}",
    "changelog": "${TEMP}",
    "description": "string",
    "user": "${USER}"
  }
}"
 
Old 04-05-2019, 08:27 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Your second example has nested double quotes. Try escaping the internal quotes, or changing them to single quotes.
 
Old 04-05-2019, 11:42 PM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Original Poster
Rep: Reputation: 42
Quote:
Originally Posted by scasey View Post
Your second example has nested double quotes. Try escaping the internal quotes, or changing them to single quotes.
thank you. Is this what you mean?

Code:
curl -X POST "https://api.json" \
     -H "X-Api-Key:9f" -i \
     -H "Content-Type: application/json" \
     -d \
'{
  \"deployment\": {
    \"revision\": \"${VER}\",
    \"changelog\": \"${TEMP}\",
    \"description\": \"string\",
    \"user\": \"${USER}\"
  }
}'
It doesn't workl
 
Old 04-05-2019, 11:53 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by cbtshare View Post
thank you. Is this what you mean?

Code:
curl -X POST "https://api.json" \
     -H "X-Api-Key:9f" -i \
     -H "Content-Type: application/json" \
     -d \
“{
  \"deployment\": {
    \"revision\": \"${VER}\",
    \"changelog\": \"${TEMP}\",
    \"description\": \"string\",
    \"user\": \"${USER}\"
  }
}”
It doesn't workl
Yes, except leave the outer double quotes...
...and please share how it “doesn’t work”. We can’t help if you don’t tell us what’s happening.

When you get internal server error, what does the error log say? (Do you have access to the logs?)

Last edited by scasey; 04-05-2019 at 11:57 PM.
 
Old 04-06-2019, 04:00 PM   #5
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Original Poster
Rep: Reputation: 42
Quote:
Originally Posted by scasey View Post
Yes, except leave the outer double quotes...
...and please share how it “doesn’t work”. We can’t help if you don’t tell us what’s happening.

When you get internal server error, what does the error log say? (Do you have access to the logs?)
Yes, I mean I get internal server error. There are no other logs on my side other than below.
Code:
curl -X POST "https://ap.json"      -H "X-Api-Key:a6f" -i      -H "Content-Type: application/json"      -d "{
 \ "deployment\": {
   \" revision\": "${VER}",
    \"changelog\": "${TEMP}",
   \" description\": "string",
    \"user\": "${USER}"
  }
}"
HTTP/1.1 500 Internal Server Error
Proxied-By: Service Gateway
Content-Security-Policy: frame-ancestors *.newrelic.com
Content-Length: 8739
Content-Type: text/html; charset=utf-8
Date: Sat, 06 Apr 2019 20:55:21 GMT
Server: nginx
Status: 500 Internal Server Error
X-Rack-Cache: invalidate, pass
X-Request-Id: 71da81fb1e0be6a3c7cee5ede0868059
X-Runtime: 0.002438
Vary: Accept-Encoding

<!DOCTYPE html>
When it works I get the following, but obviously there is no variable expansion, so the data posted is incorrect.
Code:
curl -X POST 'https://api..json'      -H 'X-Api-Key:a69f' -i      -H 'Content-Type: application/json'      -d '{
  "deployment": {
    "revision": "${VER}",
    "changelog": "${TEMP}",
    "description": "string",
    "user": "${USER}"
  }
}'
HTTP/1.1 201 Created
Proxied-By: Service Gateway
Content-Security-Policy: frame-ancestors *.newrelic.com
Cache-Control: max-age=0, private, must-revalidate
Content-Length: 251
Content-Type: application/json
Date: Sat, 06 Apr 2019 21:00:51 GMT
Etag: "0f4be74620ce63d1cf"
Server: nginx
Status: 201 Created
X-Rack-Cache: invalidate, pass
X-Request-Id: 6772f79222f62df
X-Runtime: 0.178740
X-Ua-Compatible: IE=Edge,chrome=1

{"deployment":{"id":3127,"revision":"${VER}","changelog":"${TEMP}","description":"string","user":"${USER}","timestamp":"2019-04-06T14:00:51-07:00","links":{"application":2651}},"links":{"deployment.agent":"/v2/applications/{application_id}"}}

Last edited by cbtshare; 04-06-2019 at 04:02 PM.
 
Old 04-06-2019, 04:19 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by cbtshare View Post
Yes, I mean I get internal server error. There are no other logs on my side other than below.
Code:
curl -X POST "https://ap.json"      -H "X-Api-Key:a6f" -i      -H "Content-Type: application/json"      -d "{
 \ "deployment\": {
   \" revision\": "${VER}",
    \"changelog\": "${TEMP}",
   \" description\": "string",
    \"user\": "${USER}"
  }
}"
[snip]
In this post, you didn't escape the first internal quote, but the space in front of it.

Have you tried reversing the quotes? Use double quotes around the entire -d option, and single quotes within.
Code:
-d "{
  'deployment': {
    'revision': '${VER}',
    'changelog': '${TEMP}',
    'description': 'string',
    'user': '${USER}'
  }
}"

Last edited by scasey; 04-06-2019 at 04:20 PM.
 
Old 04-08-2019, 10:57 AM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Original Poster
Rep: Reputation: 42
Hello ,

Thank you for you assistance, but that would not work, since single quotes would negate variable expansion.

The answer is to input the json into a file and then call it within the curl call
cat <<EOF> FILE.TXT
{
"deployment": {
"revision": "${VER}",
"changelog": "${TEMP}",
"description": "string",
"user": "${USER}"
}
}
EOF

Using -d `cat FILE.TXT`
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Creating Servers via REST API on RDO Mitaka via Chrome Advanced REST Client LXer Syndicated Linux News 0 04-24-2016 11:57 PM
variable expansion and pathname expansion ShadeLover Linux - General 6 04-22-2015 10:56 PM
Tcl to login to a Rest API interface Jykke Programming 1 04-03-2015 02:02 PM
Need help on my LED REST API aoiregion Programming 2 07-26-2014 01:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration