使用PowerShell的Invoke-WebRequest命令进行API调用

3
我正在使用Gitea,并尝试在PowerShell中通过API调用创建用户:

测试平台:https://try.gitea.io/

API URL:https://try.gitea.io/api/swagger

API Token:3bb81a498393f4af3d278164b5755fc23b74b785

用户名:Will-stackoverflow

密码: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变量已经正确传入:

{
    "Password":  "P@w0rd",
    "full_name":  "My User",
    "Username":  "myuser",
    "Email":  "myuser@mydomain.com",
    "source_id":  0,
    "login_name":  "myuser",
    "send_notify":  "false"
}

但是这是我的生产环境中的结果(因为我无法在在线平台上使其完全工作):

enter image description here

对我来说,听起来像是需要填写“用户名”,“电子邮件”和“密码”字段,但它们在我的显示JSON中已经填写了。我错过了什么或做错了什么?

编辑:

按照Theo的建议,将-ContentType 'application/json'参数添加到Invoke-WebRequest命令中:

enter image description here


我注意到在转换为JSON后,字段的顺序不同了。这可能是问题吗?如果是,如何保持正确的字段在正确的位置? - Will
1
一个哈希表没有特定的顺序,字段的顺序不重要。 - Theo
你尝试过将身份验证令牌添加到标头中吗?Invoke-WebRequest -H 'Authorization: token AUTH_TOKEN_HERE' - codewario
如果我正确读取错误信息,它抱怨你的JSON中“send_notify”的值。尝试将"send_notify": "false"更改为"send_notify": $false。这样它就会接收布尔值而不是字符串"false"。我已经编辑了我的答案。 - Theo
1个回答

2

看起来在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'

这是我一开始做的,但由于PowerShell输出的错误消息显示了那些大写的字段名,所以我之后添加了它们。没有运气 :/ - Will
我认为你应该在Invoke-WebRequest命令中添加-ContentType 'application/json' - Theo
我已经更新了我的帖子,并附上了我收到的新消息。我不知道我们是前进还是后退添加那行代码 x) - Will
但是您没有在命令中添加“-ContentType 'application/json'”。 - Theo
我执行了Theo,所以消息变成了:Invoke-WebRequest -uri "http://localhost:3000/api/v1/admin/users?access_token=xxx" Method POST -ContentType 'application/json' -Body $JSON - Will

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接