API - error creating access rule (10019)

Very new to the cloudflare API.
I’m trying to add rule to block IP address.
Sending request to https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules
with headers X-Auth-Email, X-Auth-Key, Content-Type.
I also added the following json as raw body: ‘{“mode”:“block”,“configuration”:{“target”:“ip”,“value”:“”}}’.
But I’m getting error response:

“result”: null,
“success”: false,
“errors”: [
“code”: 10019,
“message”: “firewallaccessrules.api.bad_json”

You either sent a bad content-type header or bad json. It looks valid based on what your put but something somewhere is breaking the json input.

1 Like

Beware of complex string substitutions; I was having some serious problems inside a bash script, because variables do not get expanded inside single quotes. Imagine you have the following scenario:

JSON = '{"mode":"block", "configuration": {"target":"IP", "value":"$IP"}}'

and then, at some point in your code, you place $JSON inside a cURL request. That will fail, because the $IP will not be expanded!

Instead, you will need to write something like:

JSON = "{\"mode\":\"block\", \"configuration\": {\"target\":\"IP\", \"value\":\"$IP\"}}"

Looks a bit crude, but it should work under bash at least.

A similar issue may happen if you are writing your code in PHP (similar syntax, similar issues), but I’d say that with PHP there are far more options to format the line (e.g. using string concatenation, using sprintf(), etc.).