Sending a POST request via curl has issues with double quotes
So, in-order to send a POST request to gists (github), you can do something like this as seen in https://gist.github.com/caspyi...
curl --user "user" -X POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists
But, in the above example, the file name and the content of the file are hard coded which is the part ..file1.txt":{"content":"Demo"}
..
I am replacing the above part with my variable $file":{"content":"$content"}
but initialize the variable, the json request must be enclosed in double quotes, which I did as
curl --user "user" -X POST --data "{"description":"Created via API","public":"true","files":{"$file":{"content":"$content"}}' https://api.github.com/gists
But this does not work, I get json error.
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3/gists/#create-a-gist"
}
Even, if I replaced all the escaped double-quotes with '
single quotes.
Does anyone know how to include a variable inside this json request?
btw: I have used all headers such as
-H "Content-Type: application/json; charset=UTF-8"
and many combinations to validate the request but to no avail
UPDATE.
This is what the entire content looks like.
function gist_controller(){
content=$(cat $1)
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
read -p "enter your password - " pass
public="false"
gist_content=$(cat $1)
curl --user "samserayo" -H "Content-Type: application/json; charset=UTF-8" -X POST -d "{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}" https://api.github.com/gists
}
curl
migrated from serverfault.com Apr 18 '16 at 20:54
This question came from our site for system and network administrators.
add a comment |
So, in-order to send a POST request to gists (github), you can do something like this as seen in https://gist.github.com/caspyi...
curl --user "user" -X POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists
But, in the above example, the file name and the content of the file are hard coded which is the part ..file1.txt":{"content":"Demo"}
..
I am replacing the above part with my variable $file":{"content":"$content"}
but initialize the variable, the json request must be enclosed in double quotes, which I did as
curl --user "user" -X POST --data "{"description":"Created via API","public":"true","files":{"$file":{"content":"$content"}}' https://api.github.com/gists
But this does not work, I get json error.
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3/gists/#create-a-gist"
}
Even, if I replaced all the escaped double-quotes with '
single quotes.
Does anyone know how to include a variable inside this json request?
btw: I have used all headers such as
-H "Content-Type: application/json; charset=UTF-8"
and many combinations to validate the request but to no avail
UPDATE.
This is what the entire content looks like.
function gist_controller(){
content=$(cat $1)
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
read -p "enter your password - " pass
public="false"
gist_content=$(cat $1)
curl --user "samserayo" -H "Content-Type: application/json; charset=UTF-8" -X POST -d "{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}" https://api.github.com/gists
}
curl
migrated from serverfault.com Apr 18 '16 at 20:54
This question came from our site for system and network administrators.
This is almost certainly better asked on Super User, as I see no real connection to server or network administration. You can flag it and request migration if you want the question moved there.
– a CVn
Apr 18 '16 at 20:15
@MichaelKjörling Thanks, flagged it. Though, I think curl request has certainly something to do with server administration.
– samayo
Apr 18 '16 at 20:18
Problems with quotes and escaping are the most basic of all programming problems (syntax errors), the question should be migrated to SO.
– Alexander
Apr 18 '16 at 21:11
add a comment |
So, in-order to send a POST request to gists (github), you can do something like this as seen in https://gist.github.com/caspyi...
curl --user "user" -X POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists
But, in the above example, the file name and the content of the file are hard coded which is the part ..file1.txt":{"content":"Demo"}
..
I am replacing the above part with my variable $file":{"content":"$content"}
but initialize the variable, the json request must be enclosed in double quotes, which I did as
curl --user "user" -X POST --data "{"description":"Created via API","public":"true","files":{"$file":{"content":"$content"}}' https://api.github.com/gists
But this does not work, I get json error.
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3/gists/#create-a-gist"
}
Even, if I replaced all the escaped double-quotes with '
single quotes.
Does anyone know how to include a variable inside this json request?
btw: I have used all headers such as
-H "Content-Type: application/json; charset=UTF-8"
and many combinations to validate the request but to no avail
UPDATE.
This is what the entire content looks like.
function gist_controller(){
content=$(cat $1)
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
read -p "enter your password - " pass
public="false"
gist_content=$(cat $1)
curl --user "samserayo" -H "Content-Type: application/json; charset=UTF-8" -X POST -d "{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}" https://api.github.com/gists
}
curl
So, in-order to send a POST request to gists (github), you can do something like this as seen in https://gist.github.com/caspyi...
curl --user "user" -X POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists
But, in the above example, the file name and the content of the file are hard coded which is the part ..file1.txt":{"content":"Demo"}
..
I am replacing the above part with my variable $file":{"content":"$content"}
but initialize the variable, the json request must be enclosed in double quotes, which I did as
curl --user "user" -X POST --data "{"description":"Created via API","public":"true","files":{"$file":{"content":"$content"}}' https://api.github.com/gists
But this does not work, I get json error.
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3/gists/#create-a-gist"
}
Even, if I replaced all the escaped double-quotes with '
single quotes.
Does anyone know how to include a variable inside this json request?
btw: I have used all headers such as
-H "Content-Type: application/json; charset=UTF-8"
and many combinations to validate the request but to no avail
UPDATE.
This is what the entire content looks like.
function gist_controller(){
content=$(cat $1)
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
read -p "enter your password - " pass
public="false"
gist_content=$(cat $1)
curl --user "samserayo" -H "Content-Type: application/json; charset=UTF-8" -X POST -d "{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}" https://api.github.com/gists
}
curl
curl
edited Apr 19 '16 at 6:54
asked Apr 18 '16 at 20:12
samayo
10818
10818
migrated from serverfault.com Apr 18 '16 at 20:54
This question came from our site for system and network administrators.
migrated from serverfault.com Apr 18 '16 at 20:54
This question came from our site for system and network administrators.
This is almost certainly better asked on Super User, as I see no real connection to server or network administration. You can flag it and request migration if you want the question moved there.
– a CVn
Apr 18 '16 at 20:15
@MichaelKjörling Thanks, flagged it. Though, I think curl request has certainly something to do with server administration.
– samayo
Apr 18 '16 at 20:18
Problems with quotes and escaping are the most basic of all programming problems (syntax errors), the question should be migrated to SO.
– Alexander
Apr 18 '16 at 21:11
add a comment |
This is almost certainly better asked on Super User, as I see no real connection to server or network administration. You can flag it and request migration if you want the question moved there.
– a CVn
Apr 18 '16 at 20:15
@MichaelKjörling Thanks, flagged it. Though, I think curl request has certainly something to do with server administration.
– samayo
Apr 18 '16 at 20:18
Problems with quotes and escaping are the most basic of all programming problems (syntax errors), the question should be migrated to SO.
– Alexander
Apr 18 '16 at 21:11
This is almost certainly better asked on Super User, as I see no real connection to server or network administration. You can flag it and request migration if you want the question moved there.
– a CVn
Apr 18 '16 at 20:15
This is almost certainly better asked on Super User, as I see no real connection to server or network administration. You can flag it and request migration if you want the question moved there.
– a CVn
Apr 18 '16 at 20:15
@MichaelKjörling Thanks, flagged it. Though, I think curl request has certainly something to do with server administration.
– samayo
Apr 18 '16 at 20:18
@MichaelKjörling Thanks, flagged it. Though, I think curl request has certainly something to do with server administration.
– samayo
Apr 18 '16 at 20:18
Problems with quotes and escaping are the most basic of all programming problems (syntax errors), the question should be migrated to SO.
– Alexander
Apr 18 '16 at 21:11
Problems with quotes and escaping are the most basic of all programming problems (syntax errors), the question should be migrated to SO.
– Alexander
Apr 18 '16 at 21:11
add a comment |
3 Answers
3
active
oldest
votes
You don't show what value you have set for $file or $content. What do those both expand out to? It's likely one, or the other, or both, contain characters the JSON parser doesn't like. Most likely, something inside the variable expansion value needs to be further escaped or encoded before submission to the JSON parser through curl.
Have you EXPORTed the environment variables?
Try:
echo "$file $content";
to make sure they are both set as you expect them to be.
Sorry, updated the content.
– samayo
Apr 19 '16 at 6:55
add a comment |
You didn't specify what shell you are using, but anything within double-quotes is subject to shell filename expansion. In particular, {} has meaning to most shells. That's the reason that the example you cited used single quotes to wrap the whole thing.
Shell escapes are tricky, and sometimes it seems impossible to get what you want. Honestly, in cases like this I would write a Python script to call curl, where I know the shell isn't "helping" me out.
Sorry, updated the content. I am using bash shell.
– samayo
Apr 19 '16 at 6:55
add a comment |
You have to put the entire string inside double quotes "
. And you need to escape quotes inside the string (not convert them into other types of quotes!).
So instead of:
"{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}"
You want:
"{ "description": "Created via API", "public": "true", "files" : { "$1" : { "content": "$gist_content"}}}"
You should probably test with echo
and make sure the string is exactly correct.
Nope, this is the first thing I did, before going crazy. It does not work like that. I always get an error as I mentioned above"message": "Problems parsing JSON",
– samayo
Apr 19 '16 at 11:56
@samayo Paste the exact message you sent. Either it's legal JSON or it isn't. (Make sure each{
has a matching}
.)
– David Schwartz
Apr 19 '16 at 16:47
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1067028%2fsending-a-post-request-via-curl-has-issues-with-double-quotes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't show what value you have set for $file or $content. What do those both expand out to? It's likely one, or the other, or both, contain characters the JSON parser doesn't like. Most likely, something inside the variable expansion value needs to be further escaped or encoded before submission to the JSON parser through curl.
Have you EXPORTed the environment variables?
Try:
echo "$file $content";
to make sure they are both set as you expect them to be.
Sorry, updated the content.
– samayo
Apr 19 '16 at 6:55
add a comment |
You don't show what value you have set for $file or $content. What do those both expand out to? It's likely one, or the other, or both, contain characters the JSON parser doesn't like. Most likely, something inside the variable expansion value needs to be further escaped or encoded before submission to the JSON parser through curl.
Have you EXPORTed the environment variables?
Try:
echo "$file $content";
to make sure they are both set as you expect them to be.
Sorry, updated the content.
– samayo
Apr 19 '16 at 6:55
add a comment |
You don't show what value you have set for $file or $content. What do those both expand out to? It's likely one, or the other, or both, contain characters the JSON parser doesn't like. Most likely, something inside the variable expansion value needs to be further escaped or encoded before submission to the JSON parser through curl.
Have you EXPORTed the environment variables?
Try:
echo "$file $content";
to make sure they are both set as you expect them to be.
You don't show what value you have set for $file or $content. What do those both expand out to? It's likely one, or the other, or both, contain characters the JSON parser doesn't like. Most likely, something inside the variable expansion value needs to be further escaped or encoded before submission to the JSON parser through curl.
Have you EXPORTed the environment variables?
Try:
echo "$file $content";
to make sure they are both set as you expect them to be.
edited Apr 18 '16 at 21:14
answered Apr 18 '16 at 21:09
JesseM
4422614
4422614
Sorry, updated the content.
– samayo
Apr 19 '16 at 6:55
add a comment |
Sorry, updated the content.
– samayo
Apr 19 '16 at 6:55
Sorry, updated the content.
– samayo
Apr 19 '16 at 6:55
Sorry, updated the content.
– samayo
Apr 19 '16 at 6:55
add a comment |
You didn't specify what shell you are using, but anything within double-quotes is subject to shell filename expansion. In particular, {} has meaning to most shells. That's the reason that the example you cited used single quotes to wrap the whole thing.
Shell escapes are tricky, and sometimes it seems impossible to get what you want. Honestly, in cases like this I would write a Python script to call curl, where I know the shell isn't "helping" me out.
Sorry, updated the content. I am using bash shell.
– samayo
Apr 19 '16 at 6:55
add a comment |
You didn't specify what shell you are using, but anything within double-quotes is subject to shell filename expansion. In particular, {} has meaning to most shells. That's the reason that the example you cited used single quotes to wrap the whole thing.
Shell escapes are tricky, and sometimes it seems impossible to get what you want. Honestly, in cases like this I would write a Python script to call curl, where I know the shell isn't "helping" me out.
Sorry, updated the content. I am using bash shell.
– samayo
Apr 19 '16 at 6:55
add a comment |
You didn't specify what shell you are using, but anything within double-quotes is subject to shell filename expansion. In particular, {} has meaning to most shells. That's the reason that the example you cited used single quotes to wrap the whole thing.
Shell escapes are tricky, and sometimes it seems impossible to get what you want. Honestly, in cases like this I would write a Python script to call curl, where I know the shell isn't "helping" me out.
You didn't specify what shell you are using, but anything within double-quotes is subject to shell filename expansion. In particular, {} has meaning to most shells. That's the reason that the example you cited used single quotes to wrap the whole thing.
Shell escapes are tricky, and sometimes it seems impossible to get what you want. Honestly, in cases like this I would write a Python script to call curl, where I know the shell isn't "helping" me out.
answered Apr 19 '16 at 2:33
FusionDude
92
92
Sorry, updated the content. I am using bash shell.
– samayo
Apr 19 '16 at 6:55
add a comment |
Sorry, updated the content. I am using bash shell.
– samayo
Apr 19 '16 at 6:55
Sorry, updated the content. I am using bash shell.
– samayo
Apr 19 '16 at 6:55
Sorry, updated the content. I am using bash shell.
– samayo
Apr 19 '16 at 6:55
add a comment |
You have to put the entire string inside double quotes "
. And you need to escape quotes inside the string (not convert them into other types of quotes!).
So instead of:
"{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}"
You want:
"{ "description": "Created via API", "public": "true", "files" : { "$1" : { "content": "$gist_content"}}}"
You should probably test with echo
and make sure the string is exactly correct.
Nope, this is the first thing I did, before going crazy. It does not work like that. I always get an error as I mentioned above"message": "Problems parsing JSON",
– samayo
Apr 19 '16 at 11:56
@samayo Paste the exact message you sent. Either it's legal JSON or it isn't. (Make sure each{
has a matching}
.)
– David Schwartz
Apr 19 '16 at 16:47
add a comment |
You have to put the entire string inside double quotes "
. And you need to escape quotes inside the string (not convert them into other types of quotes!).
So instead of:
"{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}"
You want:
"{ "description": "Created via API", "public": "true", "files" : { "$1" : { "content": "$gist_content"}}}"
You should probably test with echo
and make sure the string is exactly correct.
Nope, this is the first thing I did, before going crazy. It does not work like that. I always get an error as I mentioned above"message": "Problems parsing JSON",
– samayo
Apr 19 '16 at 11:56
@samayo Paste the exact message you sent. Either it's legal JSON or it isn't. (Make sure each{
has a matching}
.)
– David Schwartz
Apr 19 '16 at 16:47
add a comment |
You have to put the entire string inside double quotes "
. And you need to escape quotes inside the string (not convert them into other types of quotes!).
So instead of:
"{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}"
You want:
"{ "description": "Created via API", "public": "true", "files" : { "$1" : { "content": "$gist_content"}}}"
You should probably test with echo
and make sure the string is exactly correct.
You have to put the entire string inside double quotes "
. And you need to escape quotes inside the string (not convert them into other types of quotes!).
So instead of:
"{ 'description': 'Created via API', 'public': 'true', 'files':{ ' '$1 ':{ 'content': '$gist_content'}}"
You want:
"{ "description": "Created via API", "public": "true", "files" : { "$1" : { "content": "$gist_content"}}}"
You should probably test with echo
and make sure the string is exactly correct.
edited Apr 19 '16 at 16:48
answered Apr 19 '16 at 8:28
David Schwartz
56.3k684128
56.3k684128
Nope, this is the first thing I did, before going crazy. It does not work like that. I always get an error as I mentioned above"message": "Problems parsing JSON",
– samayo
Apr 19 '16 at 11:56
@samayo Paste the exact message you sent. Either it's legal JSON or it isn't. (Make sure each{
has a matching}
.)
– David Schwartz
Apr 19 '16 at 16:47
add a comment |
Nope, this is the first thing I did, before going crazy. It does not work like that. I always get an error as I mentioned above"message": "Problems parsing JSON",
– samayo
Apr 19 '16 at 11:56
@samayo Paste the exact message you sent. Either it's legal JSON or it isn't. (Make sure each{
has a matching}
.)
– David Schwartz
Apr 19 '16 at 16:47
Nope, this is the first thing I did, before going crazy. It does not work like that. I always get an error as I mentioned above
"message": "Problems parsing JSON",
– samayo
Apr 19 '16 at 11:56
Nope, this is the first thing I did, before going crazy. It does not work like that. I always get an error as I mentioned above
"message": "Problems parsing JSON",
– samayo
Apr 19 '16 at 11:56
@samayo Paste the exact message you sent. Either it's legal JSON or it isn't. (Make sure each
{
has a matching }
.)– David Schwartz
Apr 19 '16 at 16:47
@samayo Paste the exact message you sent. Either it's legal JSON or it isn't. (Make sure each
{
has a matching }
.)– David Schwartz
Apr 19 '16 at 16:47
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1067028%2fsending-a-post-request-via-curl-has-issues-with-double-quotes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This is almost certainly better asked on Super User, as I see no real connection to server or network administration. You can flag it and request migration if you want the question moved there.
– a CVn
Apr 18 '16 at 20:15
@MichaelKjörling Thanks, flagged it. Though, I think curl request has certainly something to do with server administration.
– samayo
Apr 18 '16 at 20:18
Problems with quotes and escaping are the most basic of all programming problems (syntax errors), the question should be migrated to SO.
– Alexander
Apr 18 '16 at 21:11