Forwarding_url using API V4

I feel like I’m taking crazy pills. The documentation is somewhat scant for adding a Page Rule. I’ve reasearched quite a bit, but am getting a JSON error. Here is the code I’m posting:

-X POST -H "X-Auth-Key: XXXAUTHKEYXXX" -H "X-Auth-Email: [email protected]" -H "Content-Type: application/json" --data {"targets": [{"target": "url","constraint":{"operator": "matches","value": "originaldomain.com/*"}}],"actions": [{"id": "forwarding_url","value": {"url": "https://forwardingdomain.com","status_code": 301}}],"status": "active"}

The error I’m getting is:
{“success”:false,“errors”:[{“code”:6007,“message”:“Malformed JSON in request body”}],“messages”:,“result”:null}

I’ve run this through a validator and recheck the names like 10 times. Can you see anything wrong with what I’ve got there?

This might have only happened when you posted to the forum but all your quotation marks are coming across as fancy Unicode quotes. In the code you’re actually trying to use, are they normal quotes or fancy? If I copy your JSON and search/replace with normal quotes then it validates on https://jsonlint.com/ at least

Yeah, I think it’s just the copying a pasting that made them fancy. They are definitely normal quotes. I’m able to get it to verify aa well. I’m wondering if I have the names correct (such as: forwarding_url, url, status_code, status, etc). I can’t find explicit documention that tells me exactly what to use.

Can you post it in a code block

like this

so that it hopefully won’t get modified by the forum?

three single quotes to start the code block and three single quotes to end

Cool, I just edited the original post to change it to a code block. Thank you for that advice.

your shell might be mangling it

what if you wrap the data in single quotes like --data '{...}'

or put the data in a file named temp.txt and do like --data @temp.txt

also run curl with --verbose so you can see exactly what you’re sending to the server and if it matches what you intended

except I think --verbose doesn’t show POST data but it looks like --trace-ascii log.txt would log absolutely everything (except binary data which there should be any)

1 Like

I’ve been looking up practical Cloudflare API curl examples and it seems wrapping the data in single quotes is the standard.

Like this for example, this is for cache clearing but you get the idea

(One guy used double-quotes instead but then had to backlash the double-quotes inside the data, ew gross)

That put me in the right direction. Here is the curl script that ended up working:

curl https://api.cloudflare.com/client/v4/zones/****CLOUDFLAREID***/pagerules -X POST -H "X-Auth-Key: ****CloudFlareAPI_Key**** " -H "X-Auth-Email: [email protected]" -H "Content-Type: application/json" --data '{"targets": [{"target": "url","constraint":{"operator": "matches","value": "originaldomain.com/*"}}],"actions": [{"id": "forwarding_url","value": {"url": "https://forwardtodomain.com","status_code": 301}}],"status": "active"}'

Originally I had been trying to execute this through a Filemaker database. That is still failing. However if I do it through a command line prompt it’s now working. Just a little extra coding to do it that way and all is good now. Thanks for your tips and ideas to get me there!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.