How to use ant exec task to invoke curl and POST a message to rest api












1















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="&apos;{&quot;username&quot;:&quot;xyz&quot;,&quot;password&quot;:&quot;XYZ&quot;}&apos;" />
<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









share|improve this question



























    1















    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="&apos;{&quot;username&quot;:&quot;xyz&quot;,&quot;password&quot;:&quot;XYZ&quot;}&apos;" />
    <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









    share|improve this question

























      1












      1








      1








      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="&apos;{&quot;username&quot;:&quot;xyz&quot;,&quot;password&quot;:&quot;XYZ&quot;}&apos;" />
      <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









      share|improve this question














      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="&apos;{&quot;username&quot;:&quot;xyz&quot;,&quot;password&quot;:&quot;XYZ&quot;}&apos;" />
      <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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 17 '18 at 20:00









      nihilsonnihilson

      67




      67
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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






          share|improve this answer































            0














            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="&apos;{&quot;username&quot;:&quot;xyz&quot;,&quot;password&quot;:&quot;XYZ&quot;}&apos;" />
            <arg value="https://hostname:8443/rest/api/login" />
            </exec>
            </target>





            share|improve this answer



















            • 1





              more explanation needed

              – Rai
              Jan 1 at 8:29











            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%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









            0














            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






            share|improve this answer




























              0














              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






              share|improve this answer


























                0












                0








                0







                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 5:52









                Chen DaochengChen Daocheng

                213




                213

























                    0














                    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="&apos;{&quot;username&quot;:&quot;xyz&quot;,&quot;password&quot;:&quot;XYZ&quot;}&apos;" />
                    <arg value="https://hostname:8443/rest/api/login" />
                    </exec>
                    </target>





                    share|improve this answer



















                    • 1





                      more explanation needed

                      – Rai
                      Jan 1 at 8:29
















                    0














                    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="&apos;{&quot;username&quot;:&quot;xyz&quot;,&quot;password&quot;:&quot;XYZ&quot;}&apos;" />
                    <arg value="https://hostname:8443/rest/api/login" />
                    </exec>
                    </target>





                    share|improve this answer



















                    • 1





                      more explanation needed

                      – Rai
                      Jan 1 at 8:29














                    0












                    0








                    0







                    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="&apos;{&quot;username&quot;:&quot;xyz&quot;,&quot;password&quot;:&quot;XYZ&quot;}&apos;" />
                    <arg value="https://hostname:8443/rest/api/login" />
                    </exec>
                    </target>





                    share|improve this answer













                    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="&apos;{&quot;username&quot;:&quot;xyz&quot;,&quot;password&quot;:&quot;XYZ&quot;}&apos;" />
                    <arg value="https://hostname:8443/rest/api/login" />
                    </exec>
                    </target>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 1 at 8:18









                    Tibu PadmakumarTibu Padmakumar

                    12




                    12








                    • 1





                      more explanation needed

                      – Rai
                      Jan 1 at 8:29














                    • 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


















                    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%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





















































                    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

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'