gpt4 book ai didi

java - 使用 cURL 发布 JSON 正文的 Ant exec 任务不起作用,相同的命令在 Windows 命令提示符下手动工作

转载 作者:行者123 更新时间:2023-11-30 04:07:17 25 4
gpt4 key购买 nike

我正在将此任务作为 Ant 脚本中更大目标的一部分运行。它在 Windows 命令提示符中执行命令并将输出路由到临时目录中的文本文件。我更改了 URL 参数以保护敏感信息。

<exec executable="cf.cmd" output="${java.io.tmpdir}/cmd_out.txt" failonerror="true" vmlauncher="false">
<arg value="curl" />
<arg value="POST" />
<arg value="http://www.google.com/v2/routes" />
<arg value="--body" />
<arg value="&#39;{&quot;host&quot;: &quot;${old.app.name}&quot;, &quot;domain_guid&quot;: &quot;${domain.guid}&quot;, &quot;space_guid&quot;: &quot;${space.guid}&quot;}&#39;" />
</exec>

从 Ant 的详细日志中,此任务生成以下命令:

Current OS is Windows 7
[exec] Output redirected to C:\Users\foo\AppData\Local\Temp\cmd_out.txt
[exec] Executing 'cf' with arguments:
[exec] 'curl'
[exec] 'POST'
[exec] 'http://www.google.com/v2/routes'
[exec] '--body'
[exec] ''{"host": "foo", "domain_guid": "4cc4c9b0-5b87-40f5-8ecc-6d6347a3236d", "space_guid": "6c3338e2-422d-46c0-a901-eda41b1fd968"}''
[exec]
[exec] The ' characters around the executable and arguments are
[exec] not part of the command.

运行时,此任务会在 cmd_out.txt 中生成以下输出:

{
"code": 1001,
"description": "Request invalid due to parse error: lexical error: invalid char in json text.\n {host: foo, domain_guid\n (right here) ------^\n
"error_code": "CF-MessageParseError
"types": [
"MessageParseError
"Error"
],
"backtrace": []
}

但是,如果我在 Windows 命令提示符中手动执行相同的命令:

cf curl POST http://www.google.com/v2/routes --body '{"host": "foo", "domain_guid": "4cc4c9b0-5b87-40f5-8ecc-6d6347a3236d", "space_guid": "6c3338e2-422d-46c0-a901-eda41b1fd968"}'

然后我得到了正确的结果。在此处发布它没有意义,但它是一条成功消息,而不是无效的解析错误消息。

如果我在手动 Windows 命令提示符中运行此命令(请注意 JSON 主体周围缺少单引号),那么我会得到与 Ant 脚本相同的错误:

cf curl POST http://www.google.com/v2/routes --body {"host": "foo", "domain_guid": "4cc4c9b0-5b87-40f5-8ecc-6d6347a3236d", "space_guid": "6c3338e2-422d-46c0-a901-eda41b1fd968"}

我在这里不知所措。据我所见,Ant 运行的命令与我手动执行的命令相同。然而Ant版本失败了,而手动版本成功了。谁能看出我做错了什么吗?

最佳答案

看起来问题是由空格引起的。将 exec 任务更改为以下效果非常好:

<exec executable="cf.cmd" output="${java.io.tmpdir}/cmd_out.txt" failonerror="true" vmlauncher="false">
<arg value="curl" />
<arg value="POST" />
<arg value="http://www.google.com/v2/routes" />
<arg value="--body" />
<arg value="&apos;{&quot;host&quot;:&quot;${old.app.name}&quot;,&quot;domain_guid&quot;:&quot;${domain.guid}&quot;,&quot;space_guid&quot;:&quot;${space.guid}&quot;}&apos;" />
</exec>

关于java - 使用 cURL 发布 JSON 正文的 Ant exec 任务不起作用,相同的命令在 Windows 命令提示符下手动工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20432627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com