curl:(3)[globbing]向Google Sheets API发送PUT请求时在第189列中嵌套大括号

wa7juj8i  于 2022-11-13  发布在  Go
关注(0)|答案(3)|浏览(332)

我正在尝试使用Google Sheet API v4写入Google电子表格。此处列出了有关API的用法
我创建了一个curl请求,如下所示

curl -v\
-H 'Authorization: Bearer ya29.GlxSB9-EiDh1Mn2EqhCslHvkaGyOX-P4_yDR4MXOt-WdHYQdFfwUJNMfljAFzZfS-YrrATUU2MAKj3R4BcMyOSw55KjJOC0EekE_qusj8GXIxFF3uaGZxGMdlB0IlQ' \
-X PUT \
https://sheets.googleapis.com/v4/spreadsheets/1mHrPXQILuprO4NdqTgrVKlGazvvzgCFqIphGdsmptD8/values/Sheet1\!A1:D5\?valueInputOption\='{"range": "Sheet1!A1:D5","majorDimension": "ROWS","values": [["Item", "Cost", "Stocked", "Ship Date"]

当我运行这个程序时,我得到了以下错误

curl: (3) [globbing] nested brace in column 189

我不明白我做错了什么,感谢这里的任何帮助

zphenhs4

zphenhs41#

我发现,我对API的使用是不正确的。正确使用API是

curl -v \
-H 'Authorization: Bearer ya29.GlxSB9-EiDh1Mn2EqhCslHvkaGyOX-P4_yDR4MXOt-WdHYQdFfwUJNMfljAFzZfS-YrrATUU2MAKj3R4BcMyOSw55KjJOC0EekE_qusj8GXIxFF3uaGZxGMdlB0IlQ' \
-H 'Content-Type: application/json' \
-X PUT \
-d '{"range": "Sheet1!A1:D5","majorDimension": "ROWS","values": [["Item", "Cost", "Stocked", "Ship Date"], ["Wheel", "$20.50", "4", "3/1/2016"], ["Door", "$15", "2", "3/15/2016"], ["Engine", "$100", "1", "30/20/2016"], ["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)"]]}' \
https://sheets.googleapis.com/v4/spreadsheets/1mHrPXQILuprO4NdqTgrVKlGazvvzgCFqIphGdsmptD8/values/Sheet1!A1:D5?valueInputOption=USER_ENTERED

需要注意的要点是

  • valueInputOption=USER_ENTERED。我不得不告诉API解析输入,这样USER_ENTERED就成为文档中的有效值
  • 由于有效负载是有效的json内容,因此需要使用-H 'Content-Type: application/json'
  • 使用-d选项发送有效负载。
    更正后,我就可以点击API并获得响应
*   Trying 2607:f8b0:400a:803::200a...
* TCP_NODELAY set
* Connected to sheets.googleapis.com (2607:f8b0:400a:803::200a) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-ECDSA-CHACHA20-POLY1305
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=California; L=Mountain View; O=Google LLC; CN=*.googleapis.com
*  start date: Jul  2 19:21:00 2019 GMT
*  expire date: Sep 24 18:57:00 2019 GMT
*  subjectAltName: host "sheets.googleapis.com" matched cert's "*.googleapis.com"
*  issuer: C=US; O=Google Trust Services; CN=Google Internet Authority G3
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fc02e006600)
> PUT /v4/spreadsheets/1mHrPXQILuprO4NdqTgrVKlGazvvzgCFqIphGdsmptD8/values/Sheet1!A1:D5?valueInputOption=USER_ENTERED HTTP/2
> Host: sheets.googleapis.com
> User-Agent: curl/7.54.0
> Accept: */*
> Authorization: Bearer ya29.GlxSB9-EiDh1Mn2EqhCslHvkaGyOX-P4_yDR4MXOt-WdHYQdFfwUJNMfljAFzZfS-YrrATUU2MAKj3R4BcMyOSw55KjJOC0EekE_qusj8GXIxFF3uaGZxGMdlB0IlQ
> Content-Type: application/json
> Content-Length: 272
>
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
* We are completely uploaded and fine
< HTTP/2 200
< content-type: application/json; charset=UTF-8
< vary: X-Origin
< vary: Referer
< vary: Origin,Accept-Encoding
< date: Sat, 27 Jul 2019 19:23:15 GMT
< server: ESF
< cache-control: private
< x-xss-protection: 0
< x-frame-options: SAMEORIGIN
< alt-svc: quic=":443"; ma=2592000; v="46,43,39"
< accept-ranges: none
<
{
  "spreadsheetId": "1mHrPXQILuprO4NdqTgrVKlGazvvzgCFqIphGdsmptD8",
  "updatedRange": "Sheet1!A1:D5",
  "updatedRows": 5,
  "updatedColumns": 4,
  "updatedCells": 20
}
* Connection #0 to host sheets.googleapis.com left intact
o8x7eapl

o8x7eapl2#

我收到以下错误

curl: (3) nested brace in URL position 8:

{查找所有事务处理文件{id文件名}}}'
好吧,这可能与您的服务器消息“POST body sent invalid JSON."相同,对吗?
现在,当你通过在外部使用双引号并转义内部的双引号来修复json数据时,它应该可以工作:

curl -v -d "{\"query\": \"{findAllTransactionFile {id fileName}}\"}" -H "Content-Type:application/json" http://localhost:8080/apis/graphql
9njqaruj

9njqaruj3#

如果您有带大括号的查询参数,则可以使用以下格式:

curl -X PUT \
https://sheets.googleapis.com/v4/spreadsheets/1mHrPXQILuprO4NdqTgrVKlGazvvzgCFqIphGdsmptD8/values/Sheet1\!A1:D5\ \
 --data-urlencode '{"range": "Sheet1!A1:D5","majorDimension": "ROWS","values": [["Item", "Cost", "Stocked", "Ship Date"], ["Wheel", "$20.50", "4", "3/1/2016"], ["Door", "$15", "2", "3/15/2016"], ["Engine", "$100", "1", "30/20/2016"], ["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)"]]}'

其中--data-urlencode告诉curl对URL进行编码,这样就不会有任何嵌套的大括号

相关问题