首页 文章

如何使用ant exec任务来调用curl并POST一条消息来休息api

提问于
浏览
1

创建一个Ant任务来发出一个curl post请求:

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

有趣的是,API仅支持"Content-Type: application/json" . 但curl似乎除了作为参数发送到Curl的头之外还添加了 "Content-Type: application/x-www-form-urlencoded" . API似乎不喜欢这样并返回"< HTTP/1.1 415 Unsupported Media Type" . 看起来curl正在查找编码的数据,因此自己设置此默认标头 . 那么有没有办法阻止curl设置此默认值"Content-Type: application/x-www-form-urlencoded"并只使用标头集作为curl命令的参数 .

User-Agent: curl/7.51.0
Accept: */*
 'Accept: application/json'
 'Content-Type: application/json'
Content-Length: 52
Content-Type: application/x-www-form-urlencoded

1 回答

  • 0

    虽然它很旧,你可以尝试例如:

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

    这将执行如下: curl -kiv -X POST -H "Accept: application/json" "http://localhost:${diagnose.management.port}/manage/shutdown"

    请注意:如果结果包含空格,则每个arg值似乎在结果中添加了双引号 .

    参考:https://ant.apache.org/manual/using.html#arg

相关问题