Yesterday I would like to create a new  build script on TeamCity. This build script is for calling restful web api after performance test has been done. I don't want to install any plugins on project TeamCity server, so I decide to call restful web api via powershell.


We can use "Invoke-RestMethod" to  call restful web api as below:

GET:

$header = @{
     "Authorization"="Bearer iamtokenhahaha0123456789"
}

Invoke-RestMethod -Method Get -Uri "http://localhost/api/getDate" -Header $header



POST:

$params = @{
    "Account"="IAmAccount";
    "Password"="IAmPassword"
}
$header = @{
    "Content-Type"="application/x-www-form-urlencoded"
}

Invoke-RestMethod -Method Post -Uri "http://localhost/api/login" -Body $params -Header $header



POST (json format):

$json = @{
    "UserName"="Duran"
}
$header = @{
     "Content-Type"="application/json"
}

Invoke-RestMethod -Method Post -Uri "http://localhost/api/login" -Body (ConvertTo-Json $json) -Header $header

It is simple and easy to understand, right? :)