0

I am trying to post a json string using curl in scala. My curl command works fine if executed from linux box but throes an error(("message": "Must provide query string.",) always from scala.

my working curl command in linux:

 curl http://laptpad1811:5000/graphql -H "Content-Type: application/json"  
    -X POST -d '{"query":"mutation 
    CreateFileReceivedEvent($createFileReceivedEventInput: 
    CreateFleReceivedEventInput!) {  createFileReceivedEvent(input: 
    $createFileReceivedEventInput) {    clientMutationId  }}","variables":
    {"createFileReceivedEventInput":
    {"clientMutationId":"Test","fileReceivedEvent":{"file":
    {"fileTrackingId":"83a86c44-66a5-4de0-9b7f-
    c6995877279d","name":"textfile_2017-08-21T15:58:45Z","fileType":
    {"code":"textfile"}},"eventTimestamp":"2017-08-
    21T15:59:30Z"}}},"operationName":"CreateFileReceivedEvent"}'

My scala code: step1: copying the entire json string(pay load) to txt file

 '{"query":"mutation CreateFileReceivedEvent($createFileReceivedEventInput: 
    CreateFleReceivedEventInput!) {  createFileReceivedEvent(input: 
    $createFileReceivedEventInput) {    clientMutationId  }}","variables":
    {"createFileReceivedEventInput":
    {"clientMutationId":"Test","fileReceivedEvent":{"file":
    {"fileTrackingId":"83a86c44-66a5-4de0-9b7f-
    c6995877279d","name":"textfile_2017-08-21T15:58:45Z","fileType":
    {"code":"textfile"}},"eventTimestamp":"2017-08-
    21T15:59:30Z"}}},"operationName":"CreateFileReceivedEvent"}'

step2:

 val data=fromFile("/usr/test/data.txt").getLines.mkString

step3:

val cmd = Seq("curl", "http://laptpad1811:5000/graphql", "-H",
  "'Content-Type:application/json'" ,"-X", "POST", "-d" , data)

step4:

cmd.!!

I get the below error

String =
"{
  "errors": [
    {
      "message": "Must provide query string.",
      "stack": "BadRequestError: Must provide query string.\n

I have tried to change " to ' and mutiple combinations of the json string but I always get the same error.

1 Answer 1

1

I suspect that your issue is that sys.process doesn't pass commands through the shell (e.g. bash), so quotes that are necessary in the shell become unnecessary in Scala (and get passed through to the command which in the case of Unix-style utilities will probably result in unexpected behavior).

So try:

val cmd = Seq("curl", "http://laptpad1811:5000/graphql", "-H", "Content-Type: application/json", "-X", "POST", "-d", data)

Likewise remove the single quote wrapping from your text file.

I would however, counsel against spawning a curl in Scala and advise using one of the existing http client libraries (I personally like Gigahorse).

Sign up to request clarification or add additional context in comments.

1 Comment

This worked. removing the quotes worked. Thanks Levi!!

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.