Create repository via REST-API

1,398 views
Skip to first unread message

Dan Steffen

unread,
Apr 1, 2016, 10:01:00 AM4/1/16
to Nexus Users
Hello,

at the moment I try to create a new Repository via the REST API of nexus but it didn't work and I have no idea why. I found this Thread[1] and so I create a file named new_repo.xml in the home-dir of my user. The File has the content:

<?xml version=”1.0″ encoding=”UTF-8″?>
<repository>
  <data>
    <id>my</id>
    <name>MyReleasesXml</name>
    <exposed>true</exposed>
    <repoType>hosted</repoType>
    <repoPolicy>RELEASE</repoPolicy>
    <providerRole>org.sonatype.nexus.proxy.repository.Repository</providerRole>
    <provider>maven2</provider>
    <format>maven2</format>
  </data>
</repository>

and I try to create the repository with the curl command:

curl -v -i -H "Accept: application/xml" -H "Content-Type: application/xml" -f -X POST -v -d "/home/steffen/new_repo.xml" -u admin:admin123 http://localhost:8081/nexus/service/local/repositories

but everytime I get the HTTP-Error 400 and I have no idea why. Maybe here someone can help me? Because the xml code looks good and the output of curl is not very helpful.
I would very thankful for every hint why this don't work.
best regards
Dan


[1] http://stackoverflow.com/questions/26365371/how-to-get-results-from-sonatype-nexus-rest-api-in-json-format

Rich Seddon

unread,
Apr 1, 2016, 10:32:53 AM4/1/16
to Dan Steffen, Nexus Users
Have a look here, I just answered this one recently:


Main think is, use JSON, it's what the UI uses in Nexus 2, so it's really easy to figure out the correct calls.


Rich

--
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/2de64209-4112-4aab-a7cf-08cf65165ca1%40glists.sonatype.com.
For more options, visit https://groups.google.com/a/glists.sonatype.com/d/optout.

Dan Steffen

unread,
Apr 1, 2016, 10:48:11 AM4/1/16
to Nexus Users
Hello,

thanks for the quick response. Now I try in the input-file the content:

{
    "data": {
        "repoType": "hosted",
        "id": "somerepo",
        "name": "Some Repo Name",
        "browseable": true,
        "indexable": true,
        "notFoundCacheTTL": 1440,
        "artifactMaxAge": -1,
        "metadataMaxAge": 1440,
        "itemMaxAge": 1440,
        "repoPolicy": "RELEASE",
        "provider": "maven2",
        "providerRole": "org.sonatype.nexus.proxy.repository.Repository",
        "downloadRemoteIndexes": true,
        "autoBlockActive": true,
        "fileTypeValidation": true,
        "exposed": true,
        "checksumPolicy": "WARN",
    }
}

with the curl-command:
curl -H "Content-Type: application/json" -d /home/steffen/new_repo.json -u admin:admin123 http://localhost:8081/nexus/service/local/repositories

but now I get the error:

    <h1>400 - Bad Request</h1>
    <p>Invalid XML, unable to parse using XStream</p>

and this leads me to a little bit confussion, because I already read that nexus understand json too....

Rich Seddon

unread,
Apr 1, 2016, 10:55:09 AM4/1/16
to Dan Steffen, Nexus Users
The "@" in front of the file argument is missing in your call.  That is needed, it tells curl to treat the string as a file path where it should read the content.

-d @/home/steffen/new_repo.json 

--
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.

Dan Steffen

unread,
Apr 1, 2016, 11:01:00 AM4/1/16
to Nexus Users
nexus still try to parsed as xml. I use Nexus 2.12, maybe there is a different to Nexus 3?

Dan Steffen

unread,
Apr 1, 2016, 11:01:42 AM4/1/16
to Nexus Users
My call at the moment:

curl -H "Content-Type:application/json" -d @/home/steffen/new_repo.json -X POST -u admin:admin123 http://localhost:8081/nexus/service/local/repositories

Manfred Moser

unread,
Apr 1, 2016, 11:18:09 AM4/1/16
to nexus...@glists.sonatype.com, dan.ste...@googlemail.com

Attached is an example project from the Nexus Best Practice class that does exactly that. One using json and another using xml file. And a script to call them. As well as another one to delete the repos again. 

hth

Manfred

--

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.
repomgt.zip

Rich Seddon

unread,
Apr 1, 2016, 11:33:20 AM4/1/16
to Dan Steffen, Nexus Users
That command looks right now (btw, I had accidentally left "-X POST" off my sample in the Jira ticket, but I see you corrected that).

What is the error in the nexus.log now?

--
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.

Dan Steffen

unread,
Apr 4, 2016, 3:20:35 AM4/4/16
to Nexus Users, dan.ste...@googlemail.com
Hello Manfred,

thanks for your post that what the solution of all my questions about the creation of a new repository via REST API.
best regards
Dan

rashmi nair

unread,
May 9, 2019, 5:02:49 PM5/9/19
to Nexus Users, dan.ste...@googlemail.com
Hi ,

Is anyone aware of the api to add a hosted repo to group repo (public repo) in nexus? 

Regards,
Rashmi


On Friday, 1 April 2016 11:33:20 UTC-4, Rich Seddon wrote:
That command looks right now (btw, I had accidentally left "-X POST" off my sample in the Jira ticket, but I see you corrected that).

What is the error in the nexus.log now?
On Fri, Apr 1, 2016 at 10:01 AM, 'Dan Steffen' via Nexus Users <nexus...@glists.sonatype.com> wrote:
My call at the moment:

curl -H "Content-Type:application/json" -d @/home/steffen/new_repo.json -X POST -u admin:admin123 http://localhost:8081/nexus/service/local/repositories

Am Freitag, 1. April 2016 17:01:00 UTC+2 schrieb Dan Steffen:
nexus still try to parsed as xml. I use Nexus 2.12, maybe there is a different to Nexus 3?

--
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...@glists.sonatype.com.

To post to this group, send email to nexus...@glists.sonatype.com.
Reply all
Reply to author
Forward
0 new messages