简体   繁体   English

我正在尝试使用invoke-restmethod / invoke-webrequest在powershell中执行REST API,但在传递Json输入时失败

[英]I am trying to execute REST API within powershell using invoke-restmethod/invoke-webrequest but failed when I pass Json inputs

I am trying to execute REST API within powershell using invoke-restmethod/invoke-webrequest but failed when I pass Json inputs. 我正在尝试使用invoke-restmethod / invoke-webrequest在powershell中执行REST API,但是在传递Json输入时失败。 It works with CURL command. 它与CURL命令一起使用。

curl -v --user admin:password -H Accept:application/json -H Content-Type:application/json -d "@C:\\data\\test.json" -X POST http://10.11.60.88:8081/artifactory/api/distribute curl -v-用户admin:密码-H接受:application / json -H内容类型:application / json -d“ @C:\\ data \\ test.json” -X POST http://10.11.60.88:8081 / artifactory / api / distribute

Test.json contents are as below Test.json的内容如下

{ {

"targetRepo" : "ECSDigital_Bintray", 
"packagesRepoPaths" : ["SNOW/org/apache/maven/maven-artifact/3.3.9/maven-artifact-3.3.9.jar"] 

} }

I am writting below powershell and it gives me series of errors. 我在powershell下面写东西,这给了我一系列错误。

$user = "admin" $ user =“管理员”

$pass = "password" $ pass =“密码”

$secpasswd = ConvertTo-SecureString $user -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential ($pass, $secpasswd) $ secpasswd = ConvertTo-SecureString $ user -AsPlainText -Force $ cred =新对象System.Management.Automation.PSCredential($ pass,$ secpasswd)

$postParams = @{targetRepo='SNOW';packagesRepoPaths='["org/apache/maven/maven-artifact/3.3.9/maven-artifact-3.3.9.jar"]'} $ postParams = @ {targetRepo ='SNOW'; packagesRepoPaths ='[“ org / apache / maven / maven-artifact / 3.3.9 / maven-artifact-3.3.9.jar”]'}

Invoke-WebRequest -Uri " http://10.11.60.88:8081/artifactory/api/distribute " -Credential $cred -Method Post -ContentType "application/json" -Body $postParams Invoke-WebRequest -Uri“ http://10.11.60.88:8081/artifactory/api/distribute ” -Credential $ cred -Method Post -ContentType“ application / json” -Body $ postParams

Error: Invoke-WebRequest : The remote server returned an error: (400) Bad Request. 错误:Invoke-WebRequest:远程服务器返回错误:(400)错误的请求。

I have tried some combinations of json inputs but no go. 我尝试了json输入的一些组合,但没有成功。 Any help? 有什么帮助吗?

I have fixed the issue, the following way solved my issues, there was need of additional "" 我已解决此问题,以下方法解决了我的问题,需要添加其他“”

$params = @{

uri = $ARTIFACTORY_PROTOCOL+"://"+$ARTIFACTORY_IP+":"+$ARTIFACTORY_PORT+"/artifactory/api/distribute";

Method = 'POST';

Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($ARTIFACTORY_USERNAME):$($ARTIFACTORY_PASSWORD)"));"Accept" = 'application/json';"Content-Type" = 'application/json'}

#body = @{""targetRepo"" : ""$DISTRIBUTION_REPOSITORY"", ""packagesRepoPaths"" : ""$ARTIFACTORY_PATH""} 

}

$json = "{
           ""targetRepo"":""$DISTRIBUTION_REPOSITORY"",
           ""packagesRepoPaths"":[""$ARTIFACTORY_PATH""]
         }"

$var = invoke-restmethod @params -Body $json
echo $var

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

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