none
Invoke-RestMethod returns "Error 400 Nothing is submitted" on JSon POST to Jenkins

    Question

  • I'm trying to submit job to Jenkins using Invoke-RestMethod and getting "Error 400 Nothing is submitted". Jenkins job takes 2 parameters. Here is the code:

     

    $pset = @{"parameter" = @(@{'value' = """10.0.0.22"""; 'name' = """host1"""}, @{'value' = """_JsonPostTest"""; 'name' = """tag"""} )}

    $json = ConvertTo-Json $pset -Compress

    Invoke-RestMethod $uri -Method Post -Body $json -ContentType 'application/json'

    It works just fine using curl:

    curl -X POST $uri --data-urlencode json=`'$json`' 

    Any ideas why?

    Wednesday, February 14, 2018 5:39 PM

Answers

  • Problem is most likely with Jenkins not accepting -ContentType 'application/json'. After capturing net traces I found that curl is actually posting -ContentType 'application/x-www-form-urlencoded'. The only working solution I found is:

    $pset = @{
       parameter = @(
           @{value = '10.0.0.22'; name = 'host1'},
           @{value = '_JsonPostTest'; name = 'tag'}       
       )
    }
    $json = $pset | ConvertTo-Json -Compress
    $json = "json=" + $json
    Invoke-RestMethod $uri -Method Post -Body $json -ContentType 'application/x-www-form-urlencoded'

    Previous suggestion regarding quotes and double-quotes while building parameter set is correct, but additional tweaking of json block is required.



    • Edited by pavel.lobanov Thursday, February 15, 2018 12:25 AM
    • Marked as answer by pavel.lobanov Thursday, February 15, 2018 12:31 AM
    Thursday, February 15, 2018 12:14 AM

All replies

  • This would be the correct conversion:

    $json = @{
        parameter = @(
                    @{value = '10.0.0.22'; name = 'host1'},
                    @{value = '_JsonPostTest'; name = 'tag'} 
        )
    } | ConvertTo-Json


    \_(ツ)_/

    Wednesday, February 14, 2018 6:29 PM
    Moderator
  • Still same error. it doesn't look like JSON body formatting error. It's more like curl does rest call differently than Invoke-RestMethod.
    Wednesday, February 14, 2018 6:58 PM
  • Problem is most likely with Jenkins not accepting -ContentType 'application/json'. After capturing net traces I found that curl is actually posting -ContentType 'application/x-www-form-urlencoded'. The only working solution I found is:

    $pset = @{
       parameter = @(
           @{value = '10.0.0.22'; name = 'host1'},
           @{value = '_JsonPostTest'; name = 'tag'}       
       )
    }
    $json = $pset | ConvertTo-Json -Compress
    $json = "json=" + $json
    Invoke-RestMethod $uri -Method Post -Body $json -ContentType 'application/x-www-form-urlencoded'

    Previous suggestion regarding quotes and double-quotes while building parameter set is correct, but additional tweaking of json block is required.



    • Edited by pavel.lobanov Thursday, February 15, 2018 12:25 AM
    • Marked as answer by pavel.lobanov Thursday, February 15, 2018 12:31 AM
    Thursday, February 15, 2018 12:14 AM
  • You should still start by building your Json correctly.  You method is both faulty and unnecessary.

    What Jenkins requires is not a scripting issue.  It is a product/vendor issue.


    \_(ツ)_/

    Thursday, February 15, 2018 12:25 AM
    Moderator
  • A little clarification - 'name' must begin with 'n', not 'N'.  It causes Jenkins (even in current release) errors answer :

    Jenkins [Jenkins]var isRunAsTest=false; var rootURL=""; var resURL="/static/db73cd08";crumb.init("", "");var Q=jQuery.noConflict()window.HTML5NotifierPlugin = window.HTML5NotifierPlugin || {};    window.HTML5NotifierPlugin.enabled = true;

    & etc..
    'Value' - doesn`t matter.

    Same rules is for any fileds names like 'tags_value' :

    For form parameters you can use hashtable:

    $fields = @{
        tags_value = 3333;
        title      = 'qwe';
        Remove     = $false
    }
    $jbody = "json=" + ( @{ parameter = & {$fields.Keys | % {@(@{name = $_; value = $fields[$_]})}} } | ConvertTo-Json -Depth 10)



    Legat


    • Edited by Asoik Thursday, December 6, 2018 2:15 PM
    Thursday, December 6, 2018 2:01 PM