【问题标题】:Upload artifact using Nexus API with PowerShell使用 Nexus API 和 PowerShell 上传工件
【发布时间】:2020-09-20 05:23:07
【问题描述】:

如果我想使用 Nexus API 将工件上传到存储库,有人知道请求正文是什么样的吗?

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("admin123:password123"))
$header = @{authorization = "Basic $token" }

$body = @{
    'raw.dictionary' = '/TestArtifact/Prod/'
    'raw.asset1' = 'c:\temp\lenovo.zip'
    'raw.asset1.filename' = 'lenovo.zip'

} | Convertto-Json

Invoke-RestMethod -Method POST -Uri 'http://xx.xx.xxx.xxx:8081/service/rest/v1/components?repository=TestRepo' -ContentType 'application/json'-Body $body -Headers $header

我收到“Invoke-RestMethod:响应状态代码不表示成功:415(不支持的媒体类型)”

【问题讨论】:

  • 我可以告诉你我认为哪里错了。您没有发送资产。您只是发送一个字符串,而不是文件的二进制数据。这可能就是您收到 415 错误的原因。

标签: powershell api nexus nexus3


【解决方案1】:

让我们试着解决这个问题。

您正在尝试执行的命令是

curl -v -u admin:admin123 -X POST 'http://localhost:8081/service/rest/v1/components?repository=maven-releases' -F maven2.groupId=com.google.guava -F maven2.artifactId=guava -F maven2.version=24.0-jre -F maven2.asset1=@guava-24.0-jre.jar -F maven2.asset1.extension=jar -F maven2.asset2=@guava-24.0-jre-sources.jar -F maven2.asset2.classifier=sources -F maven2.asset2.extension=jar

看起来我们的主要问题是您没有将文件转换为字节。

-F @[FileName] 是二进制文件

真正需要做的唯一改变就是改变

'raw.asset1' = 'c:\temp\lenovo.zip'

'raw.asset1' = [System.IO.File]::ReadAllBytes("c:\temp\lenovo.zip")

【讨论】:

  • 如果这不起作用,请发布您遇到的错误。如果这确实有效,请标记为正确。
猜你喜欢
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 2015-12-29
  • 2017-01-29
  • 1970-01-01
  • 2022-09-30
相关资源
最近更新 更多