简体   繁体   English

使用REST API创建Sonatype Nexus存储库会导致HTTP / 1.1 400错误请求

[英]Creating Sonatype Nexus repository using REST API causes HTTP/1.1 400 Bad Request

In my attempt to create a new Sonatype Nexus hosted repository, using the REST API, I get an "HTTP/1.1 400 Bad Request". 在我尝试使用REST API创建新的Sonatype Nexus托管存储库时,我收到了“HTTP / 1.1 400错误请求”。 I am using a Sonatype Nexus™ 2.10.0-02, running locally. 我正在使用Sonatype Nexus™2.10.0-02,在本地运行。

According to the REST API documentation, available at 根据REST API文档,可从以下位置获得

http://localhost:8081/nexus/nexus-restlet1x-plugin/default/docs/path__repositories.html

to create a new repository a POST request must look like this: 要创建新的存储库,POST请求必须如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<repository>
    <data>
       <format>...</format>
       <providerRole>...</providerRole>
       <exposed>...</exposed>
       <id>...</id>
       <name>...</name>
       <contentResourceURI>...</contentResourceURI>
       <repoType>...</repoType>
       <provider>...</provider>
    </data>
</repository>

So, my XML payload looks like this: 所以,我的XML有效负载如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<repository>
    <data>
        <format>maven2</format>
        <providerRole>org.sonatype.nexus.proxy.repository.Repository</providerRole>
        <exposed>true</exposed>
        <id>my-releases</id>
        <name>MyReleases</name>
        <contentResourceURI>http://localhost:8081/nexus/content/repositories/my-releases</contentResourceURI>
        <repoType>hosted</repoType>
        <provider>maven2</provider>
    </data>
</repository>

and up to here (I hope) is OK. 到了这里(我希望)还可以。

I use cURL to POST this XML content: 我使用cURL来发布这个XML内容:

curl -i -H "Accept: application/xml" -H "Content-Type: application/xml" -f -X POST -v -d "@$(pwd)/insert_oss_repository.xml" -u admin:admin123 http://localhost:8081/service/local/repositories

The complete log of this command is this: 这个命令的完整日志是这样的:

* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8081 (#0)
* Server auth using Basic with user 'admin'
> POST /nexus/service/local/repositories HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4xMjM=
> User-Agent: curl/7.35.0
> Host: localhost:8081
> Accept: */*
> Content-Type:application/xml
> Content-Length: 327
> 
* upload completely sent off: 327 out of 327 bytes
< HTTP/1.1 400 Bad Request
< Date: Mon, 20 Oct 2014 19:19:21 GMT
* Server Nexus/2.10.0-02 is not blacklisted
< Server: Nexus/2.10.0-02
< X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
< Set-Cookie: rememberMe=deleteMe; Path=/nexus; Max-Age=0; Expires=Sun, 19-Oct-2014 19:19:21 GMT
< Content-Type: application/xml; charset=UTF-8
< Date: Mon, 20 Oct 2014 19:19:21 GMT
* Server Noelios-Restlet-Engine/1.1.6-SONATYPE-5348-V8 is not blacklisted
< Server: Noelios-Restlet-Engine/1.1.6-SONATYPE-5348-V8
< Content-Length: 183
< 
<nexus-error>
  <errors>
    <error>
      <id>*</id>
      <msg>No enum const class org.sonatype.nexus.proxy.maven.RepositoryPolicy.null</msg>
    </error>
  </errors>
* Connection #0 to host localhost left intact
</nexus-error>

First of all, I would read your comment regarding the XML payload of my POST request. 首先,我会阅读你关于我的POST请求的XML有效负载的评论。 Second, if they took the time to create a sort of documentation, why don't they provide also a working example? 其次,如果他们花时间创建一种文档,他们为什么不提供一个工作示例呢? Third, if they made this REST API available why don't they respond with a human readable answer? 第三,如果他们使这个REST API可用,为什么他们不回答人类可读的答案?

It looks like there may be a bug in the documentation, you also need to provide the "repoPolicy" element. 看起来文档中可能存在错误,您还需要提供“repoPolicy”元素。 Here's an example payload: 这是一个示例有效负载:

<repository>
  <data>
    <id>my-releases-xml</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>

In general the best approach to use with Nexus and REST is to use the JSON payloads, these are easily discoverable because the UI calls through the REST layer and sends JSON. 一般来说,与Nexus和REST一起使用的最佳方法是使用JSON有效负载,这些很容易被发现,因为UI通过REST层调用并发送JSON。 See here for more information: 浏览此处获取更多信息:

http://blog.sonatype.com/2012/07/learning-the-nexus-rest-api-read-the-docs-or-fire-up-a-browser/ http://blog.sonatype.com/2012/07/learning-the-nexus-rest-api-read-the-docs-or-fire-up-a-browser/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM