Reply

Getting 400 (Bad Request) error while creating PTR record

[ Edited ]
Authority
Posts: 21
387     0

Hi All,

I am using powershell to create A&PTR records. while A record is getting created without any issues but for PTR records, I am getting 400(bad request) error

 

    $url = "https://ddi.st.int-go.net/wapi/v2.10.5/record:ptr?_return_fields%2B=name,ptrdname,ipv4addr&_return_as_object=1"
    $bkpptrrecord_details = @"
    {
        "name":"$bkpIPAddressFormatted",
        "ptrdname":"$RecordName",
        "ipv4addr":"$BackupIP",
        "view":"cpdz-back"
    }
"@

    $body = $bkpptrrecord_details
    $bkpptrrecord = Invoke-RestMethod -Uri $url -Method POST -Credential $creds -ContentType 'application/json' -Body $body -Verbose

the sample json 

    {
        "name":"8.19.12.1.in-addr.arpa",
        "ptrdname":"TST25DB-bkp.cly.dtc1.cf.int-in.net",
        "ipv4addr":"1.12.19.8",
        "view":"cpdz-back"
    }

Please let me know what is wrong here

Re: Getting 400 (Bad Request) error while creating PTR record

Moderator
Moderator
Posts: 321
388     0

i've been able to fix the 400 errors by enabling higher versions of TLS.     Not sure if that's your issue but it's possible.

 

Here's an example that works for me, putting the example data inline.  Note that I prefer the REQUEST call, and putting all the details in the JSON content.

 

$niosip = "192.168.1.2"
$niosuser = "admin"
$niospw = "infoblox"
$wapiver = "v2.10.5"

##########
# enable TLS 1.0, 1.1 and 1.2

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

##########
# Do the following to ignore self-signed certificates
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy



$resturl = 'https://' + $niosip + '/wapi/' + $wapiver + '/request'

$resturl

$data = '
[
  {
    "method":"POST",
    "object": "record:ptr",
    "data":{
 	"name":"8.19.12.1.in-addr.arpa",
        "ptrdname":"TST25DB-bkp.cly.dtc1.cf.int-in.net",
        "ipv4addr":"1.12.19.8",
        "view":"cpdz-back"
    }
  }
]'

$json = $data | ConvertTo-Json

$json

$cred = New-Object PSCredential $niosuser, ($niospw | ConvertTo-SecureString -AsPlainText -Force)

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    
    public class IDontCarePolicy : ICertificatePolicy {
        public IDontCarePolicy() {}
        public bool CheckValidationResult(
            ServicePoint sPoint, X509Certificate cert,
            WebRequest wRequest, int certProb) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy 

$out = Invoke-RestMethod -Uri $resturl -Method Post -Credential $cred -Body $data -ContentType 'application/json'

Re: Getting 400 (Bad Request) error while creating PTR record

Authority
Posts: 21
388     0

@MattR:  Thanks for the response. I have checked the way you provided the approch but still I am facing the issue while creating the PTR record only

Re: Getting 400 (Bad Request) error while creating PTR record

Authority
Posts: 21
388     0

We have a specific service account which we are using to access the API. Is it possible that there is some access/rights which is missing which is not allowing the user to create PTR record?

Re: Getting 400 (Bad Request) error while creating PTR record

Moderator
Moderator
Posts: 321
388     0

It is possible.  It's also possible the PTR already exists, or the reverse zone doesn't exist.  Or a handful of other situations. 

Let's put a try/catch around the call to capture the body of the error response.  Also note that either the name or the IP address is required but you don't need to include both for a traditional PTR record.  So I'm just including the IP address here.

 

Give this a try? 

 

$resturl = 'https://' + $niosip + '/wapi/' + $wapiver + '/request'

$resturl

$data = '
[
  {
    "method":"POST",
    "object": "record:ptr",
    "data":{
        "ptrdname":"TST25DB-bkp.cly.dtc1.cf.int-in.net",
        "ipv4addr":"1.12.19.8",
        "view":"cpdz-back"
    }
  }
]'

$json = $data | ConvertTo-Json

$json

$cred = New-Object PSCredential $niosuser, ($niospw | ConvertTo-SecureString -AsPlainText -Force)

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    
    public class IDontCarePolicy : ICertificatePolicy {
        public IDontCarePolicy() {}
        public bool CheckValidationResult(
            ServicePoint sPoint, X509Certificate cert,
            WebRequest wRequest, int certProb) {
            return true;
        }
    }
"@

try{
    $results = Invoke-RestMethod -Uri $resturl -Method Post -Credential $cred -Body $data -ContentType 'application/json'
    Write-Output "Results: $($results)"
    Write-Output "Created successfully $fqdn"
}
catch {
    $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
    $ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
    $streamReader.Close()
}

$ErrResp  

 

 

Showing results for 
Search instead for 
Did you mean: 

Recommended for You