How to use ant exec task to invoke curl and POST a message to rest api
Created an Ant task to make a curl post request :
<target name="invoke-curl" description="Invoke curl using Ant">
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X POST" />
<arg value="-H 'Accept: application/json'" />
<arg value="-H 'Content-Type: application/json'" />
<arg value="-d" />
<arg value="'{"username":"xyz","password":"XYZ"}'" />
<arg value="https://hostname:8443/rest/api/login" />
</exec>
</target>
Interestingly the API supports only "Content-Type: application/json". But curl seems to be adding the "Content-Type: application/x-www-form-urlencoded" in addition to the header sent as a parameter to Curl. The API does not seem to like this and returns "< HTTP/1.1 415 Unsupported Media Type". It looks like curl is finding the data encoded and hence setting this default header on its own. So is there a way to prevent curl from setting this default "Content-Type: application/x-www-form-urlencoded" and just use the header set as a parameter to curl command.
User-Agent: curl/7.51.0
Accept: */*
'Accept: application/json'
'Content-Type: application/json'
Content-Length: 52
Content-Type: application/x-www-form-urlencoded
curl post ant exec content-type
add a comment |
Created an Ant task to make a curl post request :
<target name="invoke-curl" description="Invoke curl using Ant">
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X POST" />
<arg value="-H 'Accept: application/json'" />
<arg value="-H 'Content-Type: application/json'" />
<arg value="-d" />
<arg value="'{"username":"xyz","password":"XYZ"}'" />
<arg value="https://hostname:8443/rest/api/login" />
</exec>
</target>
Interestingly the API supports only "Content-Type: application/json". But curl seems to be adding the "Content-Type: application/x-www-form-urlencoded" in addition to the header sent as a parameter to Curl. The API does not seem to like this and returns "< HTTP/1.1 415 Unsupported Media Type". It looks like curl is finding the data encoded and hence setting this default header on its own. So is there a way to prevent curl from setting this default "Content-Type: application/x-www-form-urlencoded" and just use the header set as a parameter to curl command.
User-Agent: curl/7.51.0
Accept: */*
'Accept: application/json'
'Content-Type: application/json'
Content-Length: 52
Content-Type: application/x-www-form-urlencoded
curl post ant exec content-type
add a comment |
Created an Ant task to make a curl post request :
<target name="invoke-curl" description="Invoke curl using Ant">
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X POST" />
<arg value="-H 'Accept: application/json'" />
<arg value="-H 'Content-Type: application/json'" />
<arg value="-d" />
<arg value="'{"username":"xyz","password":"XYZ"}'" />
<arg value="https://hostname:8443/rest/api/login" />
</exec>
</target>
Interestingly the API supports only "Content-Type: application/json". But curl seems to be adding the "Content-Type: application/x-www-form-urlencoded" in addition to the header sent as a parameter to Curl. The API does not seem to like this and returns "< HTTP/1.1 415 Unsupported Media Type". It looks like curl is finding the data encoded and hence setting this default header on its own. So is there a way to prevent curl from setting this default "Content-Type: application/x-www-form-urlencoded" and just use the header set as a parameter to curl command.
User-Agent: curl/7.51.0
Accept: */*
'Accept: application/json'
'Content-Type: application/json'
Content-Length: 52
Content-Type: application/x-www-form-urlencoded
curl post ant exec content-type
Created an Ant task to make a curl post request :
<target name="invoke-curl" description="Invoke curl using Ant">
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X POST" />
<arg value="-H 'Accept: application/json'" />
<arg value="-H 'Content-Type: application/json'" />
<arg value="-d" />
<arg value="'{"username":"xyz","password":"XYZ"}'" />
<arg value="https://hostname:8443/rest/api/login" />
</exec>
</target>
Interestingly the API supports only "Content-Type: application/json". But curl seems to be adding the "Content-Type: application/x-www-form-urlencoded" in addition to the header sent as a parameter to Curl. The API does not seem to like this and returns "< HTTP/1.1 415 Unsupported Media Type". It looks like curl is finding the data encoded and hence setting this default header on its own. So is there a way to prevent curl from setting this default "Content-Type: application/x-www-form-urlencoded" and just use the header set as a parameter to curl command.
User-Agent: curl/7.51.0
Accept: */*
'Accept: application/json'
'Content-Type: application/json'
Content-Length: 52
Content-Type: application/x-www-form-urlencoded
curl post ant exec content-type
curl post ant exec content-type
asked Jan 17 '18 at 20:00
nihilsonnihilson
67
67
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Though it was old, you can try e.g.:
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X" />
<arg value="POST"/>
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="http://localhost:${diagnose.management.port}/manage/shutdown" />
</exec>
This will execute as below:
curl -kiv -X POST -H "Accept: application/json" "http://localhost:${diagnose.management.port}/manage/shutdown"
Note that: each arg value seems to be added a double quote in the result if it contains a space.
refer to: https://ant.apache.org/manual/using.html#arg
add a comment |
change your code as shown below
<target name="invoke-curl" description="Invoke curl using Ant">
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X POST" />
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="-H" />
<arg value="Content-Type: application/json" />
<arg value="-d" />
<arg value="'{"username":"xyz","password":"XYZ"}'" />
<arg value="https://hostname:8443/rest/api/login" />
</exec>
</target>
1
more explanation needed
– Rai
Jan 1 at 8:29
add a comment |
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f48309083%2fhow-to-use-ant-exec-task-to-invoke-curl-and-post-a-message-to-rest-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Though it was old, you can try e.g.:
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X" />
<arg value="POST"/>
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="http://localhost:${diagnose.management.port}/manage/shutdown" />
</exec>
This will execute as below:
curl -kiv -X POST -H "Accept: application/json" "http://localhost:${diagnose.management.port}/manage/shutdown"
Note that: each arg value seems to be added a double quote in the result if it contains a space.
refer to: https://ant.apache.org/manual/using.html#arg
add a comment |
Though it was old, you can try e.g.:
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X" />
<arg value="POST"/>
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="http://localhost:${diagnose.management.port}/manage/shutdown" />
</exec>
This will execute as below:
curl -kiv -X POST -H "Accept: application/json" "http://localhost:${diagnose.management.port}/manage/shutdown"
Note that: each arg value seems to be added a double quote in the result if it contains a space.
refer to: https://ant.apache.org/manual/using.html#arg
add a comment |
Though it was old, you can try e.g.:
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X" />
<arg value="POST"/>
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="http://localhost:${diagnose.management.port}/manage/shutdown" />
</exec>
This will execute as below:
curl -kiv -X POST -H "Accept: application/json" "http://localhost:${diagnose.management.port}/manage/shutdown"
Note that: each arg value seems to be added a double quote in the result if it contains a space.
refer to: https://ant.apache.org/manual/using.html#arg
Though it was old, you can try e.g.:
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X" />
<arg value="POST"/>
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="http://localhost:${diagnose.management.port}/manage/shutdown" />
</exec>
This will execute as below:
curl -kiv -X POST -H "Accept: application/json" "http://localhost:${diagnose.management.port}/manage/shutdown"
Note that: each arg value seems to be added a double quote in the result if it contains a space.
refer to: https://ant.apache.org/manual/using.html#arg
answered Nov 22 '18 at 5:52
Chen DaochengChen Daocheng
213
213
add a comment |
add a comment |
change your code as shown below
<target name="invoke-curl" description="Invoke curl using Ant">
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X POST" />
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="-H" />
<arg value="Content-Type: application/json" />
<arg value="-d" />
<arg value="'{"username":"xyz","password":"XYZ"}'" />
<arg value="https://hostname:8443/rest/api/login" />
</exec>
</target>
1
more explanation needed
– Rai
Jan 1 at 8:29
add a comment |
change your code as shown below
<target name="invoke-curl" description="Invoke curl using Ant">
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X POST" />
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="-H" />
<arg value="Content-Type: application/json" />
<arg value="-d" />
<arg value="'{"username":"xyz","password":"XYZ"}'" />
<arg value="https://hostname:8443/rest/api/login" />
</exec>
</target>
1
more explanation needed
– Rai
Jan 1 at 8:29
add a comment |
change your code as shown below
<target name="invoke-curl" description="Invoke curl using Ant">
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X POST" />
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="-H" />
<arg value="Content-Type: application/json" />
<arg value="-d" />
<arg value="'{"username":"xyz","password":"XYZ"}'" />
<arg value="https://hostname:8443/rest/api/login" />
</exec>
</target>
change your code as shown below
<target name="invoke-curl" description="Invoke curl using Ant">
<exec executable="curl">
<arg value="-kiv" />
<arg value="-X POST" />
<arg value="-H" />
<arg value="Accept: application/json" />
<arg value="-H" />
<arg value="Content-Type: application/json" />
<arg value="-d" />
<arg value="'{"username":"xyz","password":"XYZ"}'" />
<arg value="https://hostname:8443/rest/api/login" />
</exec>
</target>
answered Jan 1 at 8:18
Tibu PadmakumarTibu Padmakumar
12
12
1
more explanation needed
– Rai
Jan 1 at 8:29
add a comment |
1
more explanation needed
– Rai
Jan 1 at 8:29
1
1
more explanation needed
– Rai
Jan 1 at 8:29
more explanation needed
– Rai
Jan 1 at 8:29
add a comment |
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.
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%2fstackoverflow.com%2fquestions%2f48309083%2fhow-to-use-ant-exec-task-to-invoke-curl-and-post-a-message-to-rest-api%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