PowerShell 调用-WebRequest | API 呼唤
PowerShell Invoke-WebRequest | API Call
我正在使用 Gitea,我尝试在 PowerShell 中使用 API 调用来创建用户:
Testing Plateforme : https://try.gitea.io/
API URL : https://try.gitea.io/api/swagger
API Token : 3bb81a498393f4af3d278164b5755fc23b74b785
Username : Will-Whosebug
Password : willwill
这是我迄今为止尝试过的方法:
# Filling my var with some data
$username="myuser"
$email="myuser@mydomain.com"
$full_name="My User"
$password="P@$$w0rd"
# Building a hash with my data
$hash = @{
Email = $($email);
full_name = $($full_name);
login_name = $($username);
Password = $($password);
send_notify = "false";
source_id = 0;
Username = $($username)
}
# Converting my hash to json format
$JSON = $hash | convertto-json
# Displaying my JSON var
$JSON
Invoke-WebRequest -uri "http://try.gitea.io/api/v1/admin/users?access_token=3bb81a498393f4af3d278164b5755fc23b74b785" -Method POST -Body $JSON
我的 $JSON
var 被正确喂养:
{
"Password": "P@w0rd",
"full_name": "My User",
"Username": "myuser",
"Email": "myuser@mydomain.com",
"source_id": 0,
"login_name": "myuser",
"send_notify": "false"
}
但是有结果(来自我的产品环境,因为我无法使用在线平台让它工作):
对我来说,"Username"
、"Email"
和 "Password"
字段听起来像是必填字段,但它们已填写在我显示的 JSON 中。我错过了什么或做错了什么?
编辑:
按照 Theo 的建议将 -ContentType 'application/json'
参数添加到 Invoke-WebRequest
命令:
查看 Swagger UI 站点,在我看来 json 必须仅包含小写字母的属性
{
"email": "myuser@mydomain.com",
"full_name": "My User",
"login_name": "myuser",
"password": "P@w0rd",
"send_notify": $true,
"source_id": 0,
"username": "myuser"
}
此外,根据 Swagger 站点,您需要声明 -ContentType。
您的代码如下所示:
# Filling my var with some data
$username="myuser"
$email="myuser@mydomain.com"
$full_name="My User"
$password="P@$$w0rd"
# Building a hash with my data
$hash = @{
email = $($email); # string
full_name = $($full_name); # string
login_name = $($username); # string
password = $($password); # string
send_notify = $false; # boolean
source_id = 0; # int
username = $($username) # string
}
# Converting my hash to json format
$JSON = $hash | ConvertTo-Json
# Displaying my JSON var
$JSON
# just to make it somewhat easier on the eyes
$token = "3bb81a498393f4af3d278164b5755fc23b74b785"
$url = "http://try.gitea.io/api/v1/admin/users?access_token=$token"
Invoke-WebRequest -uri $url -Method POST -Body $JSON -ContentType 'application/json'
我正在使用 Gitea,我尝试在 PowerShell 中使用 API 调用来创建用户:
Testing Plateforme : https://try.gitea.io/
API URL : https://try.gitea.io/api/swagger
API Token : 3bb81a498393f4af3d278164b5755fc23b74b785
Username : Will-Whosebug
Password : willwill
这是我迄今为止尝试过的方法:
# Filling my var with some data
$username="myuser"
$email="myuser@mydomain.com"
$full_name="My User"
$password="P@$$w0rd"
# Building a hash with my data
$hash = @{
Email = $($email);
full_name = $($full_name);
login_name = $($username);
Password = $($password);
send_notify = "false";
source_id = 0;
Username = $($username)
}
# Converting my hash to json format
$JSON = $hash | convertto-json
# Displaying my JSON var
$JSON
Invoke-WebRequest -uri "http://try.gitea.io/api/v1/admin/users?access_token=3bb81a498393f4af3d278164b5755fc23b74b785" -Method POST -Body $JSON
我的 $JSON
var 被正确喂养:
{
"Password": "P@w0rd",
"full_name": "My User",
"Username": "myuser",
"Email": "myuser@mydomain.com",
"source_id": 0,
"login_name": "myuser",
"send_notify": "false"
}
但是有结果(来自我的产品环境,因为我无法使用在线平台让它工作):
对我来说,"Username"
、"Email"
和 "Password"
字段听起来像是必填字段,但它们已填写在我显示的 JSON 中。我错过了什么或做错了什么?
编辑:
按照 Theo 的建议将 -ContentType 'application/json'
参数添加到 Invoke-WebRequest
命令:
查看 Swagger UI 站点,在我看来 json 必须仅包含小写字母的属性
{
"email": "myuser@mydomain.com",
"full_name": "My User",
"login_name": "myuser",
"password": "P@w0rd",
"send_notify": $true,
"source_id": 0,
"username": "myuser"
}
此外,根据 Swagger 站点,您需要声明 -ContentType。 您的代码如下所示:
# Filling my var with some data
$username="myuser"
$email="myuser@mydomain.com"
$full_name="My User"
$password="P@$$w0rd"
# Building a hash with my data
$hash = @{
email = $($email); # string
full_name = $($full_name); # string
login_name = $($username); # string
password = $($password); # string
send_notify = $false; # boolean
source_id = 0; # int
username = $($username) # string
}
# Converting my hash to json format
$JSON = $hash | ConvertTo-Json
# Displaying my JSON var
$JSON
# just to make it somewhat easier on the eyes
$token = "3bb81a498393f4af3d278164b5755fc23b74b785"
$url = "http://try.gitea.io/api/v1/admin/users?access_token=$token"
Invoke-WebRequest -uri $url -Method POST -Body $JSON -ContentType 'application/json'