Python script to upload artifact to nexus using the nexus API

6,678 views
Skip to first unread message

of...@playstudios-il.com

unread,
Aug 22, 2018, 3:50:41 AM8/22/18
to Nexus Users
Hi,
I'm trying to upload an artifact (json file) to the maven repository from a python script. I need to use the rest api and not maven.
curl command from shell is working well:

curl -X POST -u <user>:<password> "https://nexus:8081/service/rest/v1/components?repository=<repository name>" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "maven2.groupId=<group id>" -F "maven2.artifactId=<id>" -F "maven2.version=1.0.0" -F "maven2.asset1=@/path/to/asset.json;type=application/json" -F "maven2.asset1.extension=json"


I was unable to "translate" it to python, using either "requests" or "urllib2" or "urllib3", no matter what I tried. (I keep getting 400s or 500s). I prefer not using the "subprocess" command to execute it.

Has anyone succeeded in doing that or has any idea how it can be accomplished?



Thanks!

Frank Carnovale

unread,
Aug 22, 2018, 4:19:50 AM8/22/18
to Nexus Users
Identical frustrations recently trying to convert into Ruby a simple curl file upload to Nexus3.. finally worked when I made sure I set both the Content-Length header (to length of content)  and the Content-Type header (to in my case 'text/plain'; in your case probably 'application/json')

Brian Auron

unread,
Aug 22, 2018, 12:48:06 PM8/22/18
to Nexus Users
For what it's worth, I was able to  to do the following:

>>> auth = ('myUser', 'myPass')
>>> path = '/testing/stuff.tar.gz'
>>> content = open('stuff.tar.gz', 'rb').read()
>>> resp = requests.put(url+path, data=content, auth=auth)
>>> resp
<Response [201]>

Granted, this was to a RAW repository and not Maven, but it Did The Thing™.

Bradley Beck

unread,
Aug 22, 2018, 4:30:10 PM8/22/18
to Nexus Users
Something like the following should work:

import requests

params = (
    ('repository', 'maven-releases'),
)

files = {
    'maven2.groupId': (None, 'com.example'),
    'maven2.artifactId': (None, 'test-app'),
    'maven2.version': (None, '1.0.0'),
    'maven2.asset1': ('example.json', open('example.json', 'rb')),
    'maven2.asset1.extension': (None, 'json'),
}

response = requests.post('http://localhost:8081/service/rest/v1/components', params=params, files=files, auth=('admin', 'admin123'))


--
You received this message because you are subscribed to the Google Groups "Nexus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nexus-users...@glists.sonatype.com.
To post to this group, send email to nexus...@glists.sonatype.com.
To view this discussion on the web visit https://groups.google.com/a/glists.sonatype.com/d/msgid/nexus-users/5b2be829-d920-4909-a412-eec7a3ff542c%40glists.sonatype.com.
For more options, visit https://groups.google.com/a/glists.sonatype.com/d/optout.

of...@playstudios-il.com

unread,
Aug 23, 2018, 5:15:23 AM8/23/18
to Nexus Users
Thanks!
This worked well :)

divya tharsini

unread,
May 6, 2020, 12:31:39 AM5/6/20
to Nexus Users
Hi,

How to access the Nexus repository through URL.
To unsubscribe from this group and stop receiving emails from it, send an email to nexus...@glists.sonatype.com.

divya tharsini

unread,
May 6, 2020, 12:34:06 AM5/6/20
to Nexus Users
Hi,

  Without using the user and Password. is there anyother method like passing the token?

Michael Prescott

unread,
May 6, 2020, 8:29:58 AM5/6/20
to divya tharsini, Nexus Users

--
You received this message because you are subscribed to the Google Groups "Nexus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nexus-users...@glists.sonatype.com.
To view this discussion on the web visit https://groups.google.com/a/glists.sonatype.com/d/msgid/nexus-users/03346fe8-66ad-4c15-915a-360ff2f9ad54%40glists.sonatype.com.


--
Sonatype
Michael Prescott
Product Manager, NXRM
Phone: (301)684-8080

Reply all
Reply to author
Forward
0 new messages