4

I created a powershell script to call rest API. In powershell script I am calling post, get and put methods. script works fine in my machine and also works if I run powershell script directly on build agent machine. But if I create a build step in team city and call .ps1 (powershell file) POST and GET methods are working but PUT method is not working. Build is failing with error :

"Invoke-RestMethod : {"code":404,"errors":[{}]}"

I used below syntax to call PUT API

Invoke-RestMethod -Uri $memberEditUrl -Method Put -Body $memberEditBody -ContentType "application/json"

I logged this command using Write-Host and after build run I went to build log and clicked on logged API URL and called in browser and it is working.

It's only team city where this is not working.

What could be the issue?

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
Anil
  • 1,669
  • 5
  • 19
  • 44
  • 1
    Browser uses method GET by default, and you are using PUT... – daggett Mar 12 '19 at 05:17
  • @daggett I missed it to mention in question. I realized that and later tried same url using POSTMAN for PUT method. it is working. only in team city this is causing problem. – Anil Mar 12 '19 at 16:36
  • Then 404 - not found. Check your url. – daggett Mar 12 '19 at 16:45
  • @daggett I copied url from team city log in POSTMAN and just provided JSON data in body. That's where I am not able to figure it out that if URL is wrong it should not work in POSTMAN because I am coping url from teamcity log – Anil Mar 12 '19 at 17:13
  • in case it matter my API requires a parameter in URL which is a string and that string has forward slash "/" and colon ":" which I am replacing with %2F and %3A because providing API URl in Invoke-RestMethod – Anil Mar 12 '19 at 17:15
  • Does the REST API require any sort of authentication? – Nanhydrin Mar 13 '19 at 08:08
  • @Nanhydrin it requires token as query string. Which I am passing in URL – Anil Mar 13 '19 at 17:47
  • The only thing I can think of, and it's a bit of a long shot, is that it's something to do with the user that your build agent is running under. When you're manually testing it you're doing that as an interactive user. Build agents are frequently set up using one of the system accounts which have restricted permissions. You could try using Wireshark or something to see what traffic is actually being generated. – Nanhydrin Mar 13 '19 at 21:07
  • @Nanhydrin so I did further investigation. I am quite sure it is issue with "%2F" and "%3A". In my API last parameter is string. which has slash and colon. I am encoding that with "%2F". If I do that then getting error "{"code":404,"errors":[{}]}" But if I use string without slash and "%2F" then I get error "{"code":404,"errors":[{job not found}]}". which is correct because if I pass incorrect job name then I should get 404 with error message "job not found". Teamcity has special meaning for % so I tried to replace "%2F" with "%%2F" this didn't work. That's because I have 2 slashes and 1colon. – Anil Mar 14 '19 at 00:00
  • so instead of one % sign I used '%%%%2F". This didn't solve problem but now I see error 404 with error message "Job not found". – Anil Mar 14 '19 at 00:02
  • @Anil, have you tried "\%2F"? I believe the slash is for escaping characters in TeamCity. – Nanhydrin Mar 14 '19 at 21:05
  • @Nanhydrin No it didn't work. My api format is like this /api/jobs/{jobname}?token= . Same url works for GET and PUT methods if I send body with put method. Both GET and PUT were not working in teamcity. I tried one thing. If I call GET API like this: /api/jobs?jobname=&token= it works in teamcity. I tried to do same thing with PUT but it didn't work – Anil Mar 14 '19 at 23:39
  • Are you able to run a tcpdump or a Wireshark capture from the machine making the call and the api endpoint? – trebleCode Mar 15 '19 at 21:01
  • @trebleCode I don't have access to install any software on server. – Anil Mar 15 '19 at 22:38
  • create a powershell script with `Invoke-RestMethod ...` but with all parameters inline and try if it works without teamcity. I don't understand what is it `$memberEditUrl` - how you define it? Please provide more information how do you run this script from teamcity (execution mode, etc) – daggett Mar 18 '19 at 12:18

2 Answers2

1

Try not to convert the forward slash "/" and colon ":" which I am replacing with %2F and %3A Put api in single or double quotes "http://asd.com/abc:1234" sample example for demo

    $person = @{
    first='joe'
    lastname='doe'
}
$json = $person | ConvertTo-Json
$response = Invoke-RestMethod 'http://example.com/api/people/1' -Method Put -Body $json -ContentType 'application/json'
Jin Thakur
  • 2,711
  • 18
  • 15
0

My problem was resolved by replacing slash ("/") with tilda "~" for parameter value. No change for colon ":" and it worked.

Anil
  • 1,669
  • 5
  • 19
  • 44