curl 400 bad request (in bash script)












0















I trying to do execute the following script in bash



#!/bin/bash
source chaves.sh
HEAD='"X-Cachet-Token:'$CACHET_KEY'"'
SEARCH="'{"'"status"'":1,"'"id"'":"'"7"'","'"enabled"'":true}'"
echo $SEARCH

if curl -s --head --request GET http://google.com.br | grep "200 OK" > /dev/null; then
echo 'rodou'
curl -X PUT -H '"Content-Type:application/json;"' -H '"'X-Cachet-Token:$CACHET_KEY'"' -d $SEARCH $CACHET_URL/7

else
echo 'não deu'
curl -X PUT -H '"Content-Type: application/json;"' -H $x -d '{"status":1,"id":"7","enabled":true}' $CACHET_URL/7
fi


But keep receiving a 400 bad request from the server.



When i try to run the same line (echo in the script, Ctrl+c and Ctrl+v) directly in terminal, the command run without problems.



The source file have the directions to path and a variable token i need to use, but as far as i have tested is reading ok.





  • edit 1 - hidding some sensitive content



    edit 2 - posting the exit line (grabed trought Ctrl+c, Ctrl+v)




The command i neet to input in server is:




curl -X PUT -H "Content-Type:application/json;" -H
"X-Cachet-Token:4A7ixgkU4hcCWFReQ15G" -d
'{"status":1,"id":"7","enabled":true}'
http://XXX.XXX.XXX.XXX/api/v1/components/7




And the exit i grabed trought echo comand, give me the exact exit i want, but don't run inside script, only outside.



I'm a bit new to the curl, any help can be apreciate.



Sorry for the bad english and tks in advance.










share|improve this question

























  • The $SEARCH looks botched but we don't know exactly what the server expects. Can you edit the question to show a case where the query works with a static PUT request?

    – tripleee
    Nov 21 '18 at 12:15











  • Welcome to bash scripting character escape madness. To make it easier, first thing to do is start taking out reserved chars you don't need. For curl headers, just do: -H 'content-type: application/json' and not -H '"content-type: application/json"'. For X-Cachet-Token you can do header="X-Cachet-Token: ${CACHET_KEY}" and in curl -H $header

    – Alexandre Juma
    Nov 21 '18 at 16:40






  • 1





    Also you should try your curl commands (inside the script) with "--trace-ascii /dev/stdout" so you can actually see what the curl is sending (payload, url, headers, etc) to the server. You can actually see what you're doing wrong then and work on that instead of probably be changing stuff that is actually working :)

    – Alexandre Juma
    Nov 21 '18 at 16:43
















0















I trying to do execute the following script in bash



#!/bin/bash
source chaves.sh
HEAD='"X-Cachet-Token:'$CACHET_KEY'"'
SEARCH="'{"'"status"'":1,"'"id"'":"'"7"'","'"enabled"'":true}'"
echo $SEARCH

if curl -s --head --request GET http://google.com.br | grep "200 OK" > /dev/null; then
echo 'rodou'
curl -X PUT -H '"Content-Type:application/json;"' -H '"'X-Cachet-Token:$CACHET_KEY'"' -d $SEARCH $CACHET_URL/7

else
echo 'não deu'
curl -X PUT -H '"Content-Type: application/json;"' -H $x -d '{"status":1,"id":"7","enabled":true}' $CACHET_URL/7
fi


But keep receiving a 400 bad request from the server.



When i try to run the same line (echo in the script, Ctrl+c and Ctrl+v) directly in terminal, the command run without problems.



The source file have the directions to path and a variable token i need to use, but as far as i have tested is reading ok.





  • edit 1 - hidding some sensitive content



    edit 2 - posting the exit line (grabed trought Ctrl+c, Ctrl+v)




The command i neet to input in server is:




curl -X PUT -H "Content-Type:application/json;" -H
"X-Cachet-Token:4A7ixgkU4hcCWFReQ15G" -d
'{"status":1,"id":"7","enabled":true}'
http://XXX.XXX.XXX.XXX/api/v1/components/7




And the exit i grabed trought echo comand, give me the exact exit i want, but don't run inside script, only outside.



I'm a bit new to the curl, any help can be apreciate.



Sorry for the bad english and tks in advance.










share|improve this question

























  • The $SEARCH looks botched but we don't know exactly what the server expects. Can you edit the question to show a case where the query works with a static PUT request?

    – tripleee
    Nov 21 '18 at 12:15











  • Welcome to bash scripting character escape madness. To make it easier, first thing to do is start taking out reserved chars you don't need. For curl headers, just do: -H 'content-type: application/json' and not -H '"content-type: application/json"'. For X-Cachet-Token you can do header="X-Cachet-Token: ${CACHET_KEY}" and in curl -H $header

    – Alexandre Juma
    Nov 21 '18 at 16:40






  • 1





    Also you should try your curl commands (inside the script) with "--trace-ascii /dev/stdout" so you can actually see what the curl is sending (payload, url, headers, etc) to the server. You can actually see what you're doing wrong then and work on that instead of probably be changing stuff that is actually working :)

    – Alexandre Juma
    Nov 21 '18 at 16:43














0












0








0








I trying to do execute the following script in bash



#!/bin/bash
source chaves.sh
HEAD='"X-Cachet-Token:'$CACHET_KEY'"'
SEARCH="'{"'"status"'":1,"'"id"'":"'"7"'","'"enabled"'":true}'"
echo $SEARCH

if curl -s --head --request GET http://google.com.br | grep "200 OK" > /dev/null; then
echo 'rodou'
curl -X PUT -H '"Content-Type:application/json;"' -H '"'X-Cachet-Token:$CACHET_KEY'"' -d $SEARCH $CACHET_URL/7

else
echo 'não deu'
curl -X PUT -H '"Content-Type: application/json;"' -H $x -d '{"status":1,"id":"7","enabled":true}' $CACHET_URL/7
fi


But keep receiving a 400 bad request from the server.



When i try to run the same line (echo in the script, Ctrl+c and Ctrl+v) directly in terminal, the command run without problems.



The source file have the directions to path and a variable token i need to use, but as far as i have tested is reading ok.





  • edit 1 - hidding some sensitive content



    edit 2 - posting the exit line (grabed trought Ctrl+c, Ctrl+v)




The command i neet to input in server is:




curl -X PUT -H "Content-Type:application/json;" -H
"X-Cachet-Token:4A7ixgkU4hcCWFReQ15G" -d
'{"status":1,"id":"7","enabled":true}'
http://XXX.XXX.XXX.XXX/api/v1/components/7




And the exit i grabed trought echo comand, give me the exact exit i want, but don't run inside script, only outside.



I'm a bit new to the curl, any help can be apreciate.



Sorry for the bad english and tks in advance.










share|improve this question
















I trying to do execute the following script in bash



#!/bin/bash
source chaves.sh
HEAD='"X-Cachet-Token:'$CACHET_KEY'"'
SEARCH="'{"'"status"'":1,"'"id"'":"'"7"'","'"enabled"'":true}'"
echo $SEARCH

if curl -s --head --request GET http://google.com.br | grep "200 OK" > /dev/null; then
echo 'rodou'
curl -X PUT -H '"Content-Type:application/json;"' -H '"'X-Cachet-Token:$CACHET_KEY'"' -d $SEARCH $CACHET_URL/7

else
echo 'não deu'
curl -X PUT -H '"Content-Type: application/json;"' -H $x -d '{"status":1,"id":"7","enabled":true}' $CACHET_URL/7
fi


But keep receiving a 400 bad request from the server.



When i try to run the same line (echo in the script, Ctrl+c and Ctrl+v) directly in terminal, the command run without problems.



The source file have the directions to path and a variable token i need to use, but as far as i have tested is reading ok.





  • edit 1 - hidding some sensitive content



    edit 2 - posting the exit line (grabed trought Ctrl+c, Ctrl+v)




The command i neet to input in server is:




curl -X PUT -H "Content-Type:application/json;" -H
"X-Cachet-Token:4A7ixgkU4hcCWFReQ15G" -d
'{"status":1,"id":"7","enabled":true}'
http://XXX.XXX.XXX.XXX/api/v1/components/7




And the exit i grabed trought echo comand, give me the exact exit i want, but don't run inside script, only outside.



I'm a bit new to the curl, any help can be apreciate.



Sorry for the bad english and tks in advance.







bash curl http-status-code-400 bad-request






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:07







Emerson Pinto Bueno

















asked Nov 21 '18 at 12:06









Emerson Pinto BuenoEmerson Pinto Bueno

12




12













  • The $SEARCH looks botched but we don't know exactly what the server expects. Can you edit the question to show a case where the query works with a static PUT request?

    – tripleee
    Nov 21 '18 at 12:15











  • Welcome to bash scripting character escape madness. To make it easier, first thing to do is start taking out reserved chars you don't need. For curl headers, just do: -H 'content-type: application/json' and not -H '"content-type: application/json"'. For X-Cachet-Token you can do header="X-Cachet-Token: ${CACHET_KEY}" and in curl -H $header

    – Alexandre Juma
    Nov 21 '18 at 16:40






  • 1





    Also you should try your curl commands (inside the script) with "--trace-ascii /dev/stdout" so you can actually see what the curl is sending (payload, url, headers, etc) to the server. You can actually see what you're doing wrong then and work on that instead of probably be changing stuff that is actually working :)

    – Alexandre Juma
    Nov 21 '18 at 16:43



















  • The $SEARCH looks botched but we don't know exactly what the server expects. Can you edit the question to show a case where the query works with a static PUT request?

    – tripleee
    Nov 21 '18 at 12:15











  • Welcome to bash scripting character escape madness. To make it easier, first thing to do is start taking out reserved chars you don't need. For curl headers, just do: -H 'content-type: application/json' and not -H '"content-type: application/json"'. For X-Cachet-Token you can do header="X-Cachet-Token: ${CACHET_KEY}" and in curl -H $header

    – Alexandre Juma
    Nov 21 '18 at 16:40






  • 1





    Also you should try your curl commands (inside the script) with "--trace-ascii /dev/stdout" so you can actually see what the curl is sending (payload, url, headers, etc) to the server. You can actually see what you're doing wrong then and work on that instead of probably be changing stuff that is actually working :)

    – Alexandre Juma
    Nov 21 '18 at 16:43

















The $SEARCH looks botched but we don't know exactly what the server expects. Can you edit the question to show a case where the query works with a static PUT request?

– tripleee
Nov 21 '18 at 12:15





The $SEARCH looks botched but we don't know exactly what the server expects. Can you edit the question to show a case where the query works with a static PUT request?

– tripleee
Nov 21 '18 at 12:15













Welcome to bash scripting character escape madness. To make it easier, first thing to do is start taking out reserved chars you don't need. For curl headers, just do: -H 'content-type: application/json' and not -H '"content-type: application/json"'. For X-Cachet-Token you can do header="X-Cachet-Token: ${CACHET_KEY}" and in curl -H $header

– Alexandre Juma
Nov 21 '18 at 16:40





Welcome to bash scripting character escape madness. To make it easier, first thing to do is start taking out reserved chars you don't need. For curl headers, just do: -H 'content-type: application/json' and not -H '"content-type: application/json"'. For X-Cachet-Token you can do header="X-Cachet-Token: ${CACHET_KEY}" and in curl -H $header

– Alexandre Juma
Nov 21 '18 at 16:40




1




1





Also you should try your curl commands (inside the script) with "--trace-ascii /dev/stdout" so you can actually see what the curl is sending (payload, url, headers, etc) to the server. You can actually see what you're doing wrong then and work on that instead of probably be changing stuff that is actually working :)

– Alexandre Juma
Nov 21 '18 at 16:43





Also you should try your curl commands (inside the script) with "--trace-ascii /dev/stdout" so you can actually see what the curl is sending (payload, url, headers, etc) to the server. You can actually see what you're doing wrong then and work on that instead of probably be changing stuff that is actually working :)

– Alexandre Juma
Nov 21 '18 at 16:43












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411687%2fcurl-400-bad-request-in-bash-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411687%2fcurl-400-bad-request-in-bash-script%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

Npm cannot find a required file even through it is in the searched directory

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith