get 500 code when trying to create more than 1 droplet per a request

June 18, 2017 138 views
API Python CentOS

when I tried to create just one droplet using the API I succeed but when I tried to create more than 1 droplet per request I get server error (500 status code!)

the docs says use array of names and that's what I did...
"Creating multiple Droplets is very similar to creating a single Droplet, but instead of sending name as a string, send names as an array."

My code..


TOCKEN = "xxxxxxxx"
endpoint = 'https://api.digitalocean.com/v2/droplets/'
headers = {"Content-Type": "application/json", "Authorization": "Bearer {}".format(TOCKEN) }
params = {'region': "nyc3", 'size': "512mb", 'image': "centos-7-0-x64"}

def create_droplet(info, numb):

    x = {'names': ["Tempo" + str(x) for x in range(0, numb)]}

    info.update(x)
    return requests.post(endpoint,
                         headers=headers,
                         params=info)


print(create_droplet(params, 2).status_code)

1 Answer

Hi @hamo220058

It looks correct, but shouldn't you only send to https://api.digitalocean.com/v2/droplets without the / in the end?
I haven't tested, but that was just the first thing I noticed. If it doesn't work, then I'll try to run your script through my local python environment.

  • tried with just droplets, unfortunately got the same error

    • @hamo220058

      I missed a tiny mistake in your code - you're sending parameters instead of data. Simply change params=info to data=info.

      I used this as my test:

      import requests
      TOCKEN = "xxxxxxxx"
      endpoint = 'http://httpbin.org/post'
      headers = {"Content-Type": "application/json", "Authorization": "Bearer {}".format(TOCKEN) }
      postdata = {'region': "nyc3", 'size': "512mb", 'image': "centos-7-0-x64"}
      
      def create_droplet(info, numb):
      
          x = {'names': ["Tempo" + str(x) for x in range(0, numb)]}
      
          info.update(x)
          return requests.post(endpoint, headers=headers, data=info)
      
      print(create_droplet(postdata, 2).text)
      
Have another answer? Share your knowledge.