“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.
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:
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?
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.
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.