Jenkins API - Powershell and invoke-webrequest json file parameter

Hello everyone,

Actually, i’m looking after few hours to make an operation, and can’t find how to do this correctly.

To explain :
Job - Freestyle Project
Module - Powershell
Parameters - come from json file

When i write the Json code directly in my script and start the call, it works :

# 
$ParamsJSON = ConvertTo-Json -InputObject $JenkinsParams
Invoke-WebRequest -Uri "http://localhost/job/gci/build?" -Headers $BuildHeaders -Method Post  -Body @{ json = $ParamsJSON }

But if i try to get the data from an json file and use it in the body, this error appear :

################################################################
Invoke-WebRequest : HTTP ERROR 400 This page expects a form submission
URI:/job/gci/build
STATUS:400
MESSAGE:This page expects a form submission
################################################################

If anyone has an idea, thanks a lot in advance.

I don’t know powershell so just guessing. My immediate guess is that your not POSTing to the new job, but I see the -Method

Are you just trying to trigger another job to run? Its super easy in pipeline, you just use the build directive. In freestyle I think its a downstream job, but that might only run when the job is finished.

If your trying to access the jenkins web urls, I think you need a crsf token. I’m not sure thats the case if you don’t have auth on (I guess depends on what $BuildHeaders contains). But if you have auth on, you want to use a users’s api key not a user’s password to do the rquest, that’ll bypass the crsf check.

also lastly, the “This page expects a form submission” makes me think that maybe its because your trying to post json and not do a form post (x-www-form-urlencoded) with parameters not json.

Hello Halkeye,

Thank you for your answer.

Everything works fine with the API (csrf configured, call job, delete, queued list, etc…).

I only meet a problem when i try to set the $body from an external json file.

About the form post (x-www-form-urlencoded), with or without it’s the same, and at this moment i don’t find how to do this correctly.