curl查询到graphql端点的多行"--data"格式正确吗? [英] Correct format for multiline '--data' in curl query to graphql endpoint?

查看:100
本文介绍了curl查询到graphql端点的多行"--data"格式正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过curl将"--data"传递给GraphqQL API端点.

I'm passing "--data" via curl to a GraphqQL API endpoint.

我希望能够以预设"形式传递数据,例如就像在GraphiQL浏览器中一样,

I want to be able to pass the data in 'prettified' form, e.g. as in GraphiQL browser,

{
  alpha {
    param1
    param2
  }
}

自动取款机,我在数据内部 的格式-即re:行返回-处理不正确.

Atm, my formatting inside the data -- namely, re: line returns -- isn't handled properly.

此单行字符串形式有效

curl \
 -H 'content-type: application/json' \
 -X POST /path/to/graphql/api/endpoint \
 --data '{ "query":
           "query { alpha {param1, param2} } "
         }'

此美化"版本不

curl \
 -H 'content-type: application/json' \
 -X POST /path/to/graphql/api/endpoint \
 --data '{ "query":
           "query {
              alpha {
                param1
                param2
              }
            } "
         }'

传递第二种形式的正确语法是什么?

What's the right syntax for passing the 2nd form?

我猜是引号/转义的组合吗?

I'm guessing some combination of quoting/escaping?

推荐答案

JSON字符串中不允许使用换行符. (请参阅 RFC 8259§7,其中指出必须转义控制字符. )您可以将换行符转换为\n,但这有点笨拙:

Newlines just aren't allowed inside JSON strings. (See RFC 8259 §7, which states that control characters must be escaped.) You can turn a newline into \n, but that gets a little unwieldy:

curl \
 -H 'content-type: application/json' \
 -X POST /path/to/graphql/api/endpoint \
 --data '{ "query":
           "query {\n  alpha {\n    param1\n    param2\n  }\n} "
         }'

由于JSON查询的结构相当好,因此使用专用工具提交GraphQL查询似乎更好用: GraphiQL 是可以的默认设置,或者,如果您拥有带有合理的HTTP客户端的常用脚本语言,则可以很容易地用它编写一些东西.

Since the JSON queries are pretty well-structured, it seems to work better to use a dedicated tool for submitting GraphQL queries: the standalone version of GraphiQL is an okay default, or if you have a favorite scripting language with a reasonable HTTP client it's easy enough to write something with that.

这篇关于curl查询到graphql端点的多行"--data"格式正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆