2017-09-16 89 views
0

我想在scala中使用curl发布json字符串。如果从Linux框,但阵痛错误((“消息”执行我的curl命令工作得很好:“必须提供的查询字符串。”)总是从斯卡拉在linux使用卷曲在scala中传递Json字符串时发生错误

我的工作curl命令:

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"}' 

我的Scala代码: 第一步:复制整个JSON字符串(有效载荷),以txt文件

'{"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"}' 

第二步:

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

第三步:

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

第四步:

cmd.!! 

我得到下面的错误

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

我曾试图改变“,以”和JSON字符串的多发组合,但我总是得到相同的错误。

回答

0

我怀疑你的问题是,sys.process不会通过shell(例如bash)传递命令,因此shell中必要的引号在Scala中变得不必要了(并且可以通过传递给Unix样式的实用程序可能会导致意外的行为)。

所以尝试:

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

同样从文本文件中删除单引号包裹。然而,我会反对在Scala中产生卷曲,并建议使用现有的http客户端库之一(我个人比较喜欢Gigahorse)。

+0

这工作。删除引用工作。谢谢Levi! –