Create Redirect 301 Using Cloudflare API

How i create redirect 301 using Cloudflare api?

Im found this but this is not a curl?

can anyone help me?

curl -X POST “https://api.Cloudflare.com/client/v4/zones/-/rulesets”
-H "X-Auth-Email: "
-H "X-Auth-Key: "
-H “Content-Type: application/json”
-d ‘{
“name”: “Redirect rules ruleset”,
“kind”: “zone”,
“phase”: “http_request_dynamic_redirect”,
“rules”: [
{
“expression”: “(ip.geoip.country eq "GB" or ip.geoip.country eq "FR") and http.request.uri.path eq "/"”,
“description”: “Redirect GB and FR users in home page to localized site.”,
“action”: “redirect”,
“action_parameters”: {
“from_value”: {
“target_url”: {
“expression”: “lower(concat("https://", ip.geoip.country, ".example.com"))”
},
“status_code”: 307,
“preserve_query_string”: true
}
}
}
]
}’

You’ll need to escape quotes inside values such as expression. For example:

"expression": "lower(concat(\"https://\", ip.geoip.country, \".example.com\"))"

Okat tq bro

curl --request POST \
  --url https://api.cloudflare.com/client/v4/zones/3fc2442b82b90e7794c3/pagerules \
  --header "Content-Type: application/json" \
  --header "X-Auth-Email: [email protected]" \
  --header "X-Auth-Key:7acc900a1478f1ca40d2a220b" \
  --data "{
  "actions": [
    {
  "targets": [
    {
      "target": "url",
      "constraint": {
        "operator": "matches",
        "value": "adspro.me/*"
      }
    }
  ],
  "actions": [
    {
      "id": "forwarding_url",
      "value": {
        "url": "https://google.com/",
        "status_code": 301
      }
    }
  ],
  "status": "active"
}"

why i get error? anyone know?
this my error look.

{
    "success": false,
    "errors": [{
        "code": 6007,
        "message": "Malformed JSON in request body"
    }],
    "messages": [],
    "result": null
}

im trying to redirect my url but i get this error (Malformed JSON in request body)
My cooding look like this…

curl --request POST \
  --url https://api.cloudflare.com/client/v4/zones/3fc2442b82b90e7794c3/pagerules \
  --header "Content-Type: application/json" \
  --header "X-Auth-Email: [email protected]" \
  --header "X-Auth-Key:7acc900a1478f1ca40d2a220b" \
  --data "{
  "actions": [
    {
  "targets": [
    {
      "target": "url",
      "constraint": {
        "operator": "matches",
        "value": "adspro.me/*"
      }
    }
  ],
  "actions": [
    {
      "id": "forwarding_url",
      "value": {
        "url": "https://google.com/",
        "status_code": 301
      }
    }
  ],
  "status": "active"
}"```

and this my error looks

{
“success”: false,
“errors”: [{
“code”: 6007,
“message”: “Malformed JSON in request body”
}],
“messages”: ,
“result”: null
}


anyone can help me to do right?

That’s the same issue @curtis already addressed. However, this issue not being Cloudflare related but rather about your shell, this is best discussed in a more appropriate forum such as StackExchange or Reddit.

i dont get it can u show me the fix my code? please

Again, this is not a Cloudflare related issue, so I am afraid it’s off-topic for the forum in the first place. Please refer to the aforementioned forums, respectively the answer curtis provided should give you the right hint anyhow. If you have further questions, it really is best to check out the mentioned forums.

1 Like

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