使用带有单引号,空格,管道等的scala sys.process [英] Using scala sys.process with single quotes, white-space, pipes etc
问题描述
我尝试使用scala.sys.process._向我的 chronos 提交POST请求。 a>服务器与curl。因为命令的参数中有空格,所以我使用 cmd的
Seq [String]
变体!!
I am trying to use scala.sys.process._ to submit a POST request to my chronos server with curl. Because there is white space in the command's arguments, I am using the Seq[String]
variant of cmd.!!
我正在建立这样的命令:
I am building the command like so:
val cmd = Seq("curl", "-L", "-X POST", "-H 'Content-Type: application/json'", "-d " + jsonHash, args.chronosHost + "/scheduler/" + jobType)
会按预期产生
cmd: Seq[String] = List(curl, -L, -X POST, -H 'Content-Type: application/json', -d '{"schedule":"R/2014-02-02T00:00:00Z/PT24H", "name":"Scala-Post-Test", "command":"which scalac", "epsilon":"PT15M", "owner":"myemail@thecompany.com", "async":false}', localhost:4040/scheduler/iso8601)
,运行这看起来像是修改'Content-Type:application / json'
参数:
however, running this appears to mangle the 'Content-Type: application/json'
argument:
scala> cmd.!!
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 264 0 100 100 164 2157 3538 --:--:-- --:--:-- --:--:-- 54666
res21: String =
"The HTTP header field "Accept" with value "*/* 'Content-Type:application/json'" could not be parsed.
"
相比之下,调用 cmd.mkString()
并将+粘贴复制到终端可以正常工作。
which I don't understand. By contrast, calling cmd.mkString(" ")
and copy+pasting into the terminal works as expected.
curl -L -X POST -H 'Content-Type:application/json' -d '{"schedule":"R/2014-02-02T00:00:00Z/PT24H", "name":"Scala-Post-Test", "command":"which scalac", "epsilon":"PT15M", "owner":"austin@quantifind.com", "async":false}' mapr-01.dev.quantifind.com:4040/scheduler/iso8601
H参数无效,任何洞察使用单引号在sys.process ._'s!非常感谢。
I have tried numerous variations on the -H argument to no avail, any insight into using single quotes in sys.process._'s !! would be greatly appreciated.
我已经尝试过这种变化,产生一系列的错误,包括
I have tried variations on this as well, which generates a slew of errors, including
<h2>HTTP ERROR: 415</h2>
<p>Problem accessing /scheduler/iso8601. Reason:
<pre> Unsupported Media Type</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
(除了宰杀jsonHash之外,即:
(in addition to butchering the jsonHash, ie:
[1/6]: '"schedule":"R/2014-02-02T00:00:00Z/PT24H"' --> <stdout>
curl: (6) Couldn't resolve host ''"schedule"'
Which makes me think it is not interpreting the -H argument correctly
推荐答案
您需要将每个参数分成一个序列的单独元素。
You need to split each argument into a separate element of a sequence.
而不是这样:
val cmd = Seq("curl", "-L", "-X POST", "-H 'Content-Type: application/json'", "-d " + jsonHash, args.chronosHost + "/scheduler/" + jobType)
您需要写下:
val cmd = Seq("curl", "-L", "-X", "POST", "-H", "'Content-Type: application/json'", "-d " + jsonHash, args.chronosHost + "/scheduler/" + jobType)
将序列的每个元素作为参数因此 - H'Content-Type ...
看起来像 curl
的一个参数应为2。
It puts each element of a sequence as an argument on a command line. So "-H 'Content-Type...
looks like a single argument to curl
while it should be 2.
这里是一个简单的测试方法:
Here is a simple way to test:
import scala.sys.process._
val cmd = Seq("find", "/dev/null", "-name", "null") // works
// does not work: val cmd = Seq("find", "/dev/null", "-name null")
val res = cmd.!!
println(res)
这篇关于使用带有单引号,空格,管道等的scala sys.process的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!