1

I'm trying to create a service on Windows Azure using the REST API and Python. The code is as follows:

def create_service(self):
            subscription_id = self.get_user_subscription_id()
            auth = self.get_user_cert_data()

            if auth is None or subscription_id is None:
                    return [(False, "Datos de autenticacion incorrectos")]
            else:
                    key_file, cert_file = auth

            service_name = str(int(time.time()*100))

            try:

                    conn = httplib.HTTPSConnection(self.AZURE_SERVER, self.AZURE_PORT, key_file=key_file, cert_file=cert_file)
                    uri = "https://%s/%s/services/hostedservices" % (self.AZURE_SERVER,subscription_id)
                    service_create_xml = '''
    <CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure">
      <ServiceName>%s</ServiceName>
      <Label>%s</Label>
      <Description>Service %s created by the IM</Description>
      <Location>West Europe</Location>
    </CreateHostedService> 
                    ''' % (service_name, base64.b64encode(service_name), service_name )
                    conn.request('POST', uri, body = service_create_xml, headers = {'x-ms-version' : '2013-03-01', 'ContentType' : 'application/xml'})
                    resp = conn.getresponse()
            except Exception, ex:
                    self.logger.exception("Error creando el service")
                    return None

            if resp.status != 201:
                    self.logger.error("Error creando el service: Codigo " + str(resp.status) + " Reason: " + str(resp.reason))
                    return None

            return service_name

However, for some unknown reason the response is always Error 400 Bad Request. Anyone knows what am I doing wrong? Thanks in advance.

0

1 Answer 1

1

For headers here:

headers = {'x-ms-version' : '2013-03-01', 'ContentType' : 'application/xml'}

Try using Content-Type instead of ContentType

Sign up to request clarification or add additional context in comments.

2 Comments

Oh man, you 'saved' my life again. I was unable to detect the bug. Now working perfectly. Many thanks again.
Done! (in both questions). Sorry, I'm new on Stackoverflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.