JSON invalid character error message

What is the name of the domain?

What is the error number?

N/A

What is the error message?

“invalid JSON: invalid character ‘\’’ looking for beginning of value”

What is the issue you’re encountering

When attempting to use curl to disable or update a ruleset rule, I receive the invalid character error message.

What steps have you taken to resolve the issue?

I’ve verified that the API allows me to generate lists of rulesets. I’ve also successfully deleted an existing ruleset. The online documentation (Cloudflare API | Rulesets › Rules › Update An Account Or Zone Ruleset Rule) lists a hard-coded id within the JSON that I assume should be replaced with the actual rule id. I have attempted updates(PATCH) using both the hard-coded id and the id of the existing rule with the same results. I have reduced the complexity of the JSON body from including multiple elements to containing only the id and enabled fields with the same result.

What are the steps to reproduce the issue?

Test the following curl commands, substituting appropriate zoneid, rulesetid, ruleid, and token information.

curl https://api.cloudflare.com/client/v4/zones//rulesets//rules/ -H “content-type:application/json” -H “authorization:Bearer ” -X PATCH -d ‘{“id”:“”,“enabled”:false}’

curl https://api.cloudflare.com/client/v4/zones//rulesets//rules/ -H “content-type:application/json” -H “authorization:Bearer ” -X PATCH -d ‘{“id”:“3a03d665bac047339bb530ecb439a90d”,“enabled”:false}’

Screenshot of the error

The editor removed placeholder text that I’d included in my reproduce examples. I’ll try again:

curl https://api.cloudflare.com/client/v4/zones//rulesets//rules/ -H “content-type:application/json” -H “authorization:Bearer ” -X PATCH -d ‘{“id”:“”,“enabled”:false}’

curl https://api.cloudflare.com/client/v4/zones//rulesets//rules/ -H “content-type:application/json” -H “authorization:Bearer ” -X PATCH -d ‘{“id”:“3a03d665bac047339bb530ecb439a90d”,“enabled”:false}’

Third time’s the charm?

curl https://api.cloudflare.com/client/v4/zones/zoneid/rulesets/rulesetid/rules/ruleid -H “content-type:application/json” -H “authorization:Bearer token” -X PATCH -d ‘{“id”:“”,“enabled”:false}’

curl https://api.cloudflare.com/client/v4/zones/zoneid/rulesets/rulesetid/rules/ruleid -H “content-type:application/json” -H “authorization:Bearer token” -X PATCH -d ‘{“id”:“3a03d665bac047339bb530ecb439a90d”,“enabled”:false}’

You can use the Preformatted Text (ctrl+e) option in the editor to include code samples.

Thank you for that hint! I’d unsuccessfully tried blockquote.

looks like a macos smart quote issue - Replace text and punctuation in documents on Mac – Apple Support (UK)

you’d want

'{"id":"3a03d665bac047339bb530ecb439a90d","enabled":false}'

I’m not using a Mac. The pretty quotation marks above are an artifact of me being a newbie who isn’t familiar with the Cloudflare community text editor. Here is how the json actually looked in my terminal:

image

It would still be helpful if you shared your full command, either as Preformatted Text here or as a pastebin.

Looking at screenshots doesn’t really help in figuring out whether there are any wrong characters in your command.

The only value you really need to censor is the authorization token.

curl https://api.cloudflare.com/client/v4/zones/a4d241ec15f64601b2ec2450eade107f/rulesets/5752786b60a4487a9b17b500f8e97ce8/rules/30cf2fc5e4454c44807cf4e140cbd890 -H "content-type:application/json" -H "authorization:Bearer mytoken" -X PATCH -d '{"id":"30cf2fc5e4454c44807cf4e140cbd890","enabled":false}'

{
  "result": null,
  "success": false,
  "errors": [
    {
      "message": "invalid JSON: invalid character '\\'' looking for beginning of value"
    }
  ],
  "messages": []

That works fine for me, I’ve just replaced my own IDs and the token. It just errors because the input is not a valid WAF rule, which is to be expected:

-d '{"id":"c25f2ba80e3a4c7c92939bfec4deaa79","enabled":false}'
{
  "result": null,
  "success": false,
  "errors": [
    {
      "code": 20015,
      "message": "'' is not a valid value for action because the action is required to create or update a rule",
      "source": {
        "pointer": "/rules/1/action"
      }
    },
    {
      "code": 20125,
      "message": "'' is not a valid value for expression because the expression cannot be blank",
      "source": {
        "pointer": "/rules/1/expression"
      }
    }
  ],
  "messages": []
}

Have you tried a different shell or even completely different device?

Could you maybe also try using quotes + escaped quotes?

-d "{\"id\":\"c25f2ba80e3a4c7c92939bfec4deaa79\",\"enabled\":false}"

What are you using as the id in the json? The id of an existing rule or some other identifier?

I have attempted this in PowerShell (where I do a lot of other API development). I resort to curl from a normal command prompt when I need to see a friendlier error message.

It’s the same ID I use at the end of the URL, the ID of the rule I’m updating. Same as you’re doing.

Thank you, Laudian. The output that you received gave me a few clues that I needed. The schema for the RedirectRule object identifies a lot of properties as optional, but they do appear to be required, even when doing something as simple as disabling an existing rule. It is likely that the API treats updates as complete replacements rather than updating only included properties.

There is room for improvement in Cloudflare’s API documentation, including the use of a hard-coded rule id in the example where the [environmental] variable reference would be more accurate.