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
- 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. - Variable substitution failure: Variables like
$siteIdwrapped in single quotes won't be expanded by bash—they'll be sent as literal$siteIdstrings to the API. - Malformed
variablesJSON: YourrunScanVariablesis invalid JSON (it has extra colons and mismatched braces, plus a typo$scanIdinstead of$siteId). - Incorrect data formatting in curl: The
--dataargument'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 fixedscan_configuration_idsto use an array syntax ([$scanConfigId]) since it's likely expecting a list of IDs (common in GraphQL APIs). - Variables handling: Used
jqto safely construct valid JSON variables—this avoids manual quote escaping errors. If you don't havejqinstalled, you can install it via your package manager (e.g.,apt install jqorbrew install jq). - Curl data formatting:
- Wrapped the
--dataargument in double quotes to allow bash variable expansion. - Escaped quotes inside the query string using
sedto ensure valid JSON. - Removed the unnecessary quotes around
$runScanVariablessince it's already valid JSON.
- Wrapped the
- Fixed typos: Corrected
$scanIdto$siteIdand 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
相关产品推荐
相关产品推荐

