Developer

last person joined: 3 days ago 

Expand all | Collapse all

Help with Central API template creation

Jump to Best Answer
  • 1.  Help with Central API template creation

    Posted Oct 02, 2020 01:03 PM

    Hi guys

     

    Thanks for a great resource!

    I'm trying to figure out what is wrong with my script. But first, login, authorization and token fetch works find. Group creation, deletion and template deletion works. However template creation does not. I'm starting to think that I'm handling the config file upload wrong somehow.

     

    The following code returns an internal server error 500.

     

    def AC_TemplateCreate(group_name, template_name):
        params = {
            "access_token": CRED_ACCESS_TOKEN,
            "name": template_name,
            "device_type":"ArubaSwitch",
            "version":"ALL",
            "model":"ALL"
        }
    
        # URL_BASE = "https://eu-apigw.central.arubanetworks.com"
        # URL_GROUPS = "/configuration/v1/groups"
        # URL_TEMPLATES = URL_GROUPS + "/{group}/templates"
        url = URL_BASE + URL_TEMPLATES.replace('{group}', group_name)
    
        file_dict = {"file": open(DIR_SCRIPT + DIR_INDIVIDUAL_CFG + r'\\' + template_name + '.txt', 'rb')}
        headers = {'Content-type': 'multipart/form-data'}
    
        resp = requests.post(url, params=params, files=file_dict, headers=headers)
    
        if (resp.status_code != 200) and (resp.status_code != 201):
            print('Failed to create template ' + template_name + '! The code is: {}'.format(resp.status_code))
            print(resp.text)
            #sys.exit()
        else:
            print('Created template: ' + template_name)

     

    I realize this isnt all that self explanatory. But if you guys can see a better way of handling posting form data than what i'm doing here i'm curious to learn.

     

    Thanks in advance

    Cheers

     

    /Kim

    Thanks in advance



  • 2.  RE: Help with Central API template creation
    Best Answer

    Posted Oct 02, 2020 02:10 PM

    Hi Kim,

    I see you are using the requests library in Python for making requests and using the files attribute to upload the template. 
    The error is in your files_dict variable. Change it as follows:

     

    file_dict = {"template": open(DIR_SCRIPT + DIR_INDIVIDUAL_CFG + r'\\' + template_name + '.txt', 'rb')}

     

     

    Moreover, requests library will take care of the file upload headers for you, all you would then need, is to add the Authorization header with the token, before making the final request ( you can also keep the Content-Type as "application/json", by default).

    Let me know if it works and if I can help you with anything else.

    Thanks,
    Jay



  • 3.  RE: Help with Central API template creation

    Posted Oct 03, 2020 08:03 AM

    Hi Jay

     

    Thanks, you just saved my life lol! But the solution wasn't exactly what you said. I fiddled at bit with the code and found that leaving my file_dict unchanged and simply removed the header from the request did the job!

     

    I would like to understand your line of thought with the file_dict though. From what I can see the difference between your file_dict and mine is that yours has a key named template as opposed to my file key. Is there a significance to that in the request?

     

    Cheers and thanks a bunch

    /Kim



  • 4.  RE: Help with Central API template creation

    Posted Oct 03, 2020 08:13 AM

    Hi Jay

     

    Just realized it only works with the template key in there! It is an expected parameter in the form part.

     

    Otherwise it returns error 400.

     

    Awesome, thanks for helping out!

     

    Cheers and have a great weekend!

    /Kim