Freeman Freeman - 6 months ago 40
Scala Question

Executing Curl command in scala

I am trying to execute the curl command through scala for scalatest,

curl -H "Content-Type: application/json" --data @/home/examples/demo/demo.json http://localhost:9090/job

which works aas expected while I tried doing it with scala ,like

import scala.sys.process._
val json = getClass.getClassLoader.getResource(arg0).getPath

val cmd = Seq("curl", "-H", "'Content-Type: application/json'","-d", s"@$json","http://localhost:9090/job")

and it produces following error

Expected 'application/json'

Answer Source

You're quoting too much:

Seq("curl", "-H", "'Content-Type: application/json'","-d", s"@$json","http://localhost:9090/job")
//                !^!                            !^!

Should be:

Seq("curl", "-H", "Content-Type: application/json", "-d", s"@$json","http://localhost:9090/job")
//                 !                            !

The reason you need to quote the content-type in the shell is because the shell will break it up if it isn't quoted. Curl doesn't know how to deal with quotes, because quotes aren't it's business. Scala won't do any word-splitting, either, because that's your job.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download