Question

API V2 PHP Domain update says domain record is not included

Posted March 1, 2015 4.1k views

Hey there,

I have strange problem here.

I started writing script yesterday for updating a domain name, and it was working fine.
But today I am getting error:

string(81) "{"id":"unprocessable_entity","message":"Record type is not included in the list"}" {"type":"A","name":"home","data":"162.10.66.0"}

This is the code:

  $ch = curl_init($request_uri);
  $headers = array(
    'Content-Type:application/json',
    'Authorization: Bearer ' . file_get_contents('token.txt') );
  $rawdata = array(
  'type'      => 'A',
  'name'      => 'sub',
  'data'      => '162.10.66.0',
  'priority'  => null,
  'port'      => null,
  'weight'    => null
);
  $postdata = json_encode($rawdata);

  $ch = curl_init();                    // initiate curl
  $url = "https://api.digitalocean.com/v2/domains/mydomain.org/records"; 
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_POST, true); 
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  $output = curl_exec ($ch); // execute
  curl_close ($ch); // close curl handle
  var_dump($output); // show output
  echo $postdata;//show what I am posting

There is no difference how I format the json POST request - it never detects the type and returns that error.

Any help will appreciated.

Thanks,
Ivan

3 comments
  • Hi! Using nearly identical code, I am able to successfully create an A record. Could you describe the environment this is being run on at all? PHP version, OS, etc…

  • Hey asb,

    Thanks a lot for the comment.

    Tried the code on both my localhost and a droplet here, and got the same result.
    Here is the specs-
    Localhost:
    Ubuntu 14.04.2 LTS 3.13.0-46-generic;
    Apache/2.4.7 (Ubuntu);
    PHP 5.5.9-1ubuntu4.6;

    VPS:
    Ubuntu 12.04.5 LTS 3.2.0-24-virtual;
    PHP 5.3.10-1ubuntu3.15;
    Apache/2.2.22 (Ubuntu);
    varnishd (varnish-3.0.2 revision cbf1284);

    Also there is a error in the posted code here - it should be without the first $ch

    Can you share the code that is working for you?
    Well of course without the API and the actual request URL.

    I also posted a question in stackoverflow as I thinking there should be something wrong with my code equivalent request from CLI is working, but nobody was able to tell this for now.
    You can see it here - http://stackoverflow.com/questions/28789266/php-curl-json-post-returns-error

    Many thanks,
    Ivan

  • Ok, for me this API is not working properly.

    Played around with domain update.
    When I request something from existing subdomain at

    https://api.digitalocean.com/v2/domains/domain.org/records/5625553

    I got response

    {"domain_record":{"id":5625553,"type":"A","name":"home","data":"37.139.14.87","priority":null,"port":null,"weight":null}}

    And when I try to update the very same domain name I got not found!

    {"id":"not_found","message":"The resource you were accessing could not be found."}

    As described here: https://developers.digitalocean.com/documentation/v2/#update-a-domain-record

    How can the ID can’t be found as I am requesting to the very same URL?

    Look for a commit to your API around 26-28 of February - before that everything was working fine.

    Thanks,
    Ivan

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Hi hozza,

At the end I think I used exec to run shell command through php, but later I gave up on php and I just have shell script which I use to update the DNS, here it is if that is going to help you:

#!/bin/bash
#Description: Update IP address for A record on Digital Ocean API v2.
#Author: Ivan Denkov

TOKEN="47537ab13b726ae38bc90181778678678567jhffjhgfh"
RECORD_ID="0000000"
DOMAIN_NAME="domain.com"

#Retrieve current public IP
NIP=$(curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//')

#Retrieve record IP
CIP=$(curl -X GET -H 'Content-Type: application/json' -H 'Authorization: Bearer '$TOKEN'' "https://api.digitalocean.com/v2/domains/"$DOMAIN_NAME"/records/"$RECORD_ID"" | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["domain_record"]["data"]')
#echo [$(date +"%d-%m-%Y-%T")] "DO WAN IP: " $CIP  >> log.txt


#Compare past WAN IP and with current WAN IP
if [ "$NIP" != "$CIP" ]; then
   curl -X PUT -H 'Content-Type: application/json' -H 'Authorization: Bearer '$TOKEN'' -d '{"data":"'"$NIP"'"}' "https://api.digitalocean.com/v2/domains/"$DOMAIN_NAME"/records/"$RECORD_ID
else
  echo "IP is the same!"
fi
exit 0;

I’m having the exact same issue… was there ever a solution to this?

  • I had the same problem in Python 2.7 with the python-digitalocean package. Ultimately, I discovered that the problem was that my token contained a trailing newline. With many information-getting APIs this doesn’t seem to matter, but for this API it does matter. I assume that the newline ends up creating broken HTTP headers.

    In Python terms, this meant I needed to .strip() whitespace from the token I read from a file. Now my domain updates work.

Submit an Answer