unable to upload certificate to Panorama via post API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements

unable to upload certificate to Panorama via post API

L1 Bithead

Hello, 

 

I am restructuring my python script and with the new structure it is failing to upload certificate to Panorama. 

 

I have a working API call: 

requests.post("https://" + ip + "/api/?&type=import&category=certificate&certificate-name="+certName +"&format=pem&key=" + panapikey, files=file, verify=False)
 
My new structure looks like this: 
 
class Panorama:
    def __makeapi_request(self, url, data="", files=""😞
        try:
            response = requests.post(url, data, files, verify=False, timeout=10)
            response.raise_for_status()
        except requests.exceptions.ConnectTimeout as err:
            print("connection error")
            exit(1)
        except requests.HTTPError as err:
            self.error = err
#            return self.error
        except BaseException:
            dom = ET.fromstring(response.content)
            self.error = dom.find('.//msg').text
#            return self.error
        else:
            self.response_xml = response.content
            self.response_httpcode = response.status_code
#            return self.response_xml

    def import_cert(self, certName😞
        files = {"file": open(certName, "rb")}
        url = f"https://{self.ip}/api/?"
        params = {"type":"import","category":"certificate","certificate-name":certName,"format":"pem", "key": self.panapikey}
        self.__makeapi_request(url, files=files, data=params)

When I run import_cer(certName) I am always getting the error bellow: 
<response status = 'error' code = '400'><result><msg>No file uploaded</msg></result></response>
 
I am copy/pasting the values from the working API call, so there should be no typos, also I am testing the import with the same certificate and the same certificate variable. 
Can someone advise what I am doing wrong ? 
1 accepted solution

Accepted Solutions

L5 Sessionator

Hi @Krasen.kolev, I was able to get the code working with changing

response = requests.post(urldatafilesverify=Falsetimeout=10)

to

response = requests.post(url, data=data, files=files, verify=False, timeout=10)

although there may be other/better ways to modify the code.

 

Hope that helps!

Help the community: "Like" helpful comments, and click "Accept as Solution" if you found your answer 🙂

View solution in original post

4 REPLIES 4

L5 Sessionator

Hi @Krasen.kolev, I was able to get the code working with changing

response = requests.post(urldatafilesverify=Falsetimeout=10)

to

response = requests.post(url, data=data, files=files, verify=False, timeout=10)

although there may be other/better ways to modify the code.

 

Hope that helps!

Help the community: "Like" helpful comments, and click "Accept as Solution" if you found your answer 🙂

Hi @JimmyHolland

 

Thanks for your reply! 

My issue is that when I use directly the post request, everything is working fine. However, I am modifying my code to start using the private function "__makeapi_request". 

so when I call function import_cert(certName), which in turn is calling __makeapi_request(url, files=files, data=params) I get the error:
"
<response status = 'error' code = '400'><result><msg>No file uploaded</msg></result></response>".

 

To me it seems that when I use the private function(__makeapi_request), Panorama does not take into account the provided file.

I tried multiple different variations to pass the data, but all of them resulted with the error above. 

L5 Sessionator

Yes, the modification I mentioned is within the __makeapi_request function's code

 

Here is my test of the code: https://github.com/jamesholland-uk/playground/blob/main/python/upload-cert-to-panorama.py

Help the community: "Like" helpful comments, and click "Accept as Solution" if you found your answer 🙂

Sorry, I didn't realized it. 

Just tested it and it is working fine! Thanks for the help! 

 

  • 1 accepted solution
  • 2461 Views
  • 4 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!