1

I'm executing this command in terminal and it works just fine:

curl -d "sentence=Jack sold the car to Jenny" austen.cs.illinois.edu:8082/parse

On the other hand executing things don't seem to be working in Scala:

import sys.process._
val output = s"""curl -d \"sentence=Jack sold the car to Jenny\" austen.cs.illinois.edu:8082/parse""".!!
println(output.split("\t"))

which outputs:

[error]   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
[error]                                  Dload  Upload   Total   Spent    Left  Speed
[error] 
[error]   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: sold
[error] curl: (6) Could not resolve host: the
[error] 
[error]   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: car
[error] curl: (6) Could not resolve host: to
[error] curl: (6) Could not resolve host: Jenny"
[error] 
[error] 100    37  100    23  100    14    153     93 --:--:-- --:--:-- --:--:--   153

Thoughts why I see different behaviors here?

1 Answer 1

7

As per documentation of scala.sys.process, execution of a string assumes that arguments are separated by spaces, and there is no way to escape embedded spaces.

What to Run and How

...

Implicitly, each process is created either out of a String, with arguments separated by spaces -- no escaping of spaces is possible -- or out of a scala.collection.Seq, where the first element represents the command name, and the remaining elements are arguments to it. In this latter case, arguments may contain spaces.

Your curl request body argument contains embedded spaces, so it is assuming that they are multiple space-delimited arguments. Since there is no escaping possible, you would have to execute it as a Seq instead, with each element explicitly specifying where there is a new argument and where there is a single argument with embedded spaces.

val output = Seq("curl", "-d", "sentence=Jack sold the car to Jenny", "austen.cs.illinois.edu:8082/parse").!!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.