API & Integration

Reply
Highlighted

Powershell RESTAPI newbie - Add a host record

chandhru
Techie
Posts: 3
904     0

Team,

 

I am trying to add an host record in my infoblox dns server using invoke-restmethod in powershell. I followed some examples and documentation. I keep getting "(400) Bad Request." errors. I can go to WebUI and can add the host record, but through API it fails. Any help is highly appreciated.

 

# Ignore SSL cert Exception

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}


$data = '{
    "name": "Server666.aaaa.net",
    "ipv4addrs": [
        {
            "ipv4addr":"10.201.0.2"                
           
        }
    ]
}'


$baseUri = "https://aaa.aaa.aaa.aaa/wapi/v2.6.1"
$uri = "$baseUri/record:host"

$cred = Get-Credential

Invoke-RestMethod -Uri $uri -Method POST -Credential $cred -ContentType "application/json" -Body $data -Verbose

Re: Powershell RESTAPI newbie - Add a host record

chandhru
Techie
Posts: 3
905     0

Error Message received

 

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At \\Infoblox\test-createhost.ps1:22 char:1
+ Invoke-RestMethod -Uri $uri -Method POST -Credential $cred -ContentTy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Re: Powershell RESTAPI newbie - Add a host record

Authority
Posts: 20
905     0

Hello,

 

Your script is fine, I was able to run it perfectly.  Based on the error, the issue seems to be with the host record you are attempting to create.  There is a better explanation of the error in the body of the returned error.  You can use this code snip around your request to display the returned error body:

 

 

try {
  $result = Invoke-RestMethod -Uri $uri -Method POST -Credential $cred -ContentType "application/json" -Body $data -Verbose
}
catch
{
        $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();
        Write-Host $responseBody
}

 

 

Re: Powershell RESTAPI newbie - Add a host record

chandhru
Techie
Posts: 3
905     0

 

Thanks Richard. Also I had to modify the following to get it working

 

$host_details = @{

name= "host-10-200-254-13.xxxx.xxx"

ipv4addrs= @(@{ipv4addr="10.200.254.13"})

 

}

 
Showing results for 
Search instead for 
Do you mean 

Recommended for You