Udhay
Udhay

Reputation: 86

Upload to nexus3 using python requests

I have a python script where I'm trying to upload an asset to nexus 3 using python requests library. The code I displayed below works for nexus 2. But as the rest api is changed for nexus 3 I'm finding difficulty to upload the assets to nexus 3.

I modified my payload to included the repository as well as asset1 file but it doesn't work. I also tried to include the header and converted to payload to json format, but no luck.

Any leads will be appreciated.

import requests

filename = 'content.zip'
url = "https://nexus3-url/repos/service/rest/v1/components?repository=maven-repo"

files = { 'filename': open(filename, 'rb') }
payload = {
                'groupId' : 'group.id',
                'asset1.extension' : 'zip',
                'version' : '1.0.0',
                'artifactId' : 'test',
                'generate-pom' : 'false'
        }


response = requests.post(url,
                        allow_redirects = False,
                        auth = requests.auth.HTTPBasicAuth(username, password),
                        files = files,
                        data = payload,
                        timeout = 20,
                        verify = cert,
                )

The error message I get is requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url:

Upvotes: 1

Views: 4504

Answers (1)

Udhay
Udhay

Reputation: 86

Found a solution. As a GAV parameters are different for nexus 3, this should be the way.

import requests

filename = 'content.zip'
payload = {
        'maven2.groupId': (None, 'group.id'),
        'maven2.artifactId': (None, 'test'),
        'maven2.version': (None, '1.0.0'),
        'maven2.generate-pom': (None, 'false'),
        'maven2.packaging': (None, 'zip'),
        'maven2.asset1': (filename, open(filename, 'rb')),
        'maven2.asset1.extension': (None, 'zip'),
}
params = (
        ('repository', 'maven-repo'),
)
url = "https://nexus3-url/repos/service/rest/v1/components"

response = requests.post(url,
         allow_redirects = False,
         auth = requests.auth.HTTPBasicAuth(username, password),
         params = params,
         files = payload,
         timeout = 20,
         verify = cert,
)

Upvotes: 4

Related Questions