Creating and Updating SRV in API using Powershell


So I’ve been working on getting the Cloudflare API to work using Powershell and I’ve hit recently a bit of a snag.
About a month ago I was able to create and Update SRV records with no issue, but now it doesn’t work anymore and I can’t see why this isn’t working anymore.
(Periods in the body has been removed due to limited links allowed)

$Body = @{
type = “SRV”
data = @{
service = “_test”
proto = “_tcp”
name = “test com”
weight = “100”
port = “5060”
target = “test test com”
priority = “10”

Running the body through a conversion to JSON and it comes out like this, which should be fine, and has been until recently:

“type”: “SRV”,
“data”: {
“service”: “_test”,
“proto”: “_tcp”,
“name”: “test com”,
“weight”: “100”,
“port”: “5060”,
“target”: “test test com”,
“priority”: “10”

Running this through: Invoke-RestMethod -Method Post -Uri “” -Headers $Header -Body ($Body | ConvertTo-JSON) -ContentType “Application/JSON”

Im getting an error of the type 400 Bad Request

I want to mention that this is working fine for other records although the body on those are a bit different.
Anyone got any idea where I’m going wrong?

I’ve also tried using curl through linux bash, but that doesn’t work either, although it’s been a while since I’ve used this so I might have forgotten how to do this properly.

curl -X POST “
-H “X-Auth-Key: xxxxxxxxxxxxxxxxxxx”
-H “X-Auth-Email: hostmaster@xxxxxxxx”
-H “Content-Type: application/json”
–data ‘{“type”:“SRV”,“service”:“_test2”,“proto”:“_tcp”,“name”:“”,“priority”:“10”,“weight”:“100”,“port”:“5061”,“target”:“”,}’

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

Try a payload that looks like this?

curl -X POST ""$zone_id"/dns_records" \ -H "X-Auth-Email: "$auth_mail \ -H "X-Auth-Key: "$auth_key \ -H "Content-Type: application/json" \ --data '{"zone_name":"","zone_id":"'$zone_id'","type":"SRV","name":"","content":"SRV 0 0 8080","data":{"priority":0,"weight":0,"port":8080,"target":"","service":"_whatyouwant","proto":"_tcp","name":""},"proxied":false,"proxiable":false,"ttl":300,"priority":1}'


Was able to get this solved with help from support.
For the problem in PS, The payload contained priority, port and weight as strings, that caused the problem.

Same problem for curl, and that there should’ve been a second array containing everything, but type.

–data ‘{“type”:“SRV”,“data”:{“service”:"_test",“proto”:"_tcp",“name”:“”,“priority”:10,“weight”:100,“port”:5061,“target”:“”}}’


I’ve stumbled across your post as I’m running into the same issues.

Would you be able to provide an updated PS code snippet, showing how you encoded the priority, port & weight within the rest of the code?

Thank you.

Not sure if it comes across properly in the latest reply, but the problem here is that I was passing priority, weight and port into the body as string values while it was expecting a number.

So make sure that the data you’re passing is not a strin (int worked fine).

So you can store the data in a variable and use a method to convert it to an int type.

So the body creation looks like this.

$Body = @{

            type = "SRV"
            data = @{

Hope that helps

1 Like

That’s brilliant, thank you.