Vishal
Vishal

Reputation: 1492

Executing Curl command in scala

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

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")
    cmd.!

and it produces following error

Expected 'application/json'

Upvotes: 4

Views: 12556

Answers (1)

HTNW
HTNW

Reputation: 29193

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 its business. Scala won't do any word-splitting, either, because that's your job.

Upvotes: 4

Related Questions