You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Curl调用GraphQL API返回200但JSON格式错误的Shell脚本排查请求

Fixing Your Shell Script + GraphQL Query Syntax Issues

Let's break down the issues in your script that are causing the JSON parsing error, and fix them step by step:

Key Problems Identified

  1. Incorrect quote escaping: You're using HTML entities (") instead of proper shell/JSON quote escaping. Bash doesn't recognize " as a quote character, so this breaks the JSON structure the GraphQL API receives.
  2. Variable substitution failure: Variables like $siteId wrapped in single quotes won't be expanded by bash—they'll be sent as literal $siteId strings to the API.
  3. Malformed variables JSON: Your runScanVariables is invalid JSON (it has extra colons and mismatched braces, plus a typo $scanId instead of $siteId).
  4. Incorrect data formatting in curl: The --data argument's JSON structure is mixing literal variable names with unexpanded bash variables, leading to invalid JSON syntax.

Corrected Script

#!/bin/bash
BSEE_WEB_SERVER_DNS=https://mybsee.example.com
BSEE_API_KEY=abc123
siteId=1
scanConfigId=456

# Properly format the mutation query with correct quotes (no HTML entities)
runScanQuery='mutation CreateScheduleItem($siteId: ID!, $scanConfigId: ID!) {
  create_schedule_item(input: {site_id: $siteId, scan_configuration_ids: [$scanConfigId]}) {
    schedule_item { id }
  }
}'

# Correct variables JSON structure (matches the query's defined variables)
runScanVariables=$(jq -n --arg siteId "$siteId" --arg scanConfigId "$scanConfigId" '{
  siteId: $siteId,
  scanConfigId: $scanConfigId
}')

runScanOperationName='CreateScheduleItem'

# Use double quotes for the curl data to allow variable expansion, and properly escape internal quotes
curl -i --request POST \
  --url "${BSEE_WEB_SERVER_DNS}/graphql/v1" \
  --header "Authorization: ${BSEE_API_KEY}" \
  --header 'Content-Type: application/json' \
  --data "{
    \"query\": \"$(printf '%s' "$runScanQuery" | sed 's/"/\\"/g')\",
    \"variables\": $runScanVariables,
    \"operationName\": \"$runScanOperationName\"
  }"

What Changed?

  • Query structure: Added variable definitions to the mutation ($siteId: ID!, $scanConfigId: ID!) so we can pass variables cleanly instead of interpolating directly into the query string. Also fixed scan_configuration_ids to use an array syntax ([$scanConfigId]) since it's likely expecting a list of IDs (common in GraphQL APIs).
  • Variables handling: Used jq to safely construct valid JSON variables—this avoids manual quote escaping errors. If you don't have jq installed, you can install it via your package manager (e.g., apt install jq or brew install jq).
  • Curl data formatting:
    • Wrapped the --data argument in double quotes to allow bash variable expansion.
    • Escaped quotes inside the query string using sed to ensure valid JSON.
    • Removed the unnecessary quotes around $runScanVariables since it's already valid JSON.
  • Fixed typos: Corrected $scanId to $siteId and fixed mismatched braces in variables.

Alternative (Without jq)

If you can't use jq, you can manually construct the variables string with proper escaping:

runScanVariables="{\"siteId\":\"$siteId\",\"scanConfigId\":\"$scanConfigId\"}"

This should resolve the "Invalid JSON" error you're seeing, as the API will now receive properly formatted JSON with expanded variable values.

内容的提问来源于stack exchange,提问作者hotmeatballsoup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:34:08