none
Invoke-RestMethod POST.... not posting, but seemingly doing GET. RRS feed

  • Question

  • $guid = [guid]::NewGuid()

    $json =  [ordered]@{
    schema = ".maxis.job.export:2014-05-01"
    type = "documentSearch"
    authorizations = @([ordered] @{
    id = "$guid"
    effect = "ALLOW"
    action = "CAN_READ"
    principal = "kerberos:user@domain.com"
    resource = "this"
    })
    parameters = [ordered] @{
    search = [ordered] @{
    schemaType = "com.amazon.maxis.issue"
    q = "containingFolder:ab-5021-4551-a050-06"
    sort = "lastUpdatedConversationDate%20desc"
    }
    exportData = [ordered] @{
    type = "table"
    columns = @(
    @{columnId = "issueid"},
    @{columnId = "title"},
    @{columnId ="status"},
    @{columnId = "nextstepaction"},
    @{columnId = "assignedfolderlabel"},
    @{columnId = "labels"},
    @{columnId = "requesteridentity"},
    @{columnId = "createdate"},
    @{columnId = "lastupdateddate"},
    @{columnId = "lastresolveddate"},
    @{columnId = "assigneeidentity"},
    @{columnId = "priority"},
    @{columnId = "rank"},
    @{columnId = "planningestimate"},
    @{columnId = "estimatedstartdate"},
    @{columnId = "estimatedcompletiondate"},
    @{columnId = "actualstartdate"},
    @{columnId = "actualcompletiondate"},
    @{columnId = "needbydate"},
    @{columnId = "issueurl"},
    @{columnId = "submitteridentity"},
    @{columnId = "resolvedbyidentity"},
    [ordered] @{path = "/customFields/string/customer/value"
    outputFieldName="Customer%20(string)"},
    [ordered] @{path = "/customFields/string/tshirt_size/value"
    outputFieldName="Tshirt%20Size%20(string)"})
    }
    exportFiles = @([ordered] @{
    id="csv"
    format="csv"}
    )
    exportContent = @{
    type="issues"
    }
        }
    } | ConvertTo-Json -Depth 6

    $response = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType 'application/json' -Body $json -Uri  https://maxis-service-prod.domain.com/jobs -Verbose

    Write-Host $response

    Okay, first is that as far as I can tell my $json variable is correct and should output the correctly formatted JSON that the API expects to receive.  The problem is that even with the -Method Post there the response is coming back with the same error message I see when I issue a GET request (which is expected because it is not a GET the API needs).

    This API requires that I POST the JSON listed and it is supposed to respond with JSON so that I can get a document ID out of the returned JSON to retrieve a generated file from the API.  The POST doesn't seem to be actually posting though.  Is there a reason that the POST method would not be working and instead acting like a GET?

    Is there a better way to do this in powershell?

    • Moved by Bill_Stewart Friday, January 26, 2018 3:24 PM Unanswerable drive-by question
    Friday, December 1, 2017 3:42 PM

All replies

  • You will have to ask the service provider to help you understand how to use their API.


    \_(ツ)_/

    Friday, December 1, 2017 7:01 PM
  • You will have to ask the service provider to help you understand how to use their API.


    \_(ツ)_/


    I have and they claim that powershell is sending a GET request instead of a POST request.
    Saturday, December 2, 2017 2:54 PM
  • If you are sending "POST" then they are wrong.  We cannot help you with this.  It is between you and the third party service people to work out how to send the correct Json with a POST.

    You can also set up a network packet sniffer to prove you are sending a POST and capture the exact Json being sent.


    \_(ツ)_/

    Saturday, December 2, 2017 11:43 PM
  • If you are sending "POST" then they are wrong.  We cannot help you with this.  It is between you and the third party service people to work out how to send the correct Json with a POST.

    You can also set up a network packet sniffer to prove you are sending a POST and capture the exact Json being sent.


    \_(ツ)_/

    Maybe, maybe not; we did figure something out though.

    we had to do a session cookie.  

    Invoke-WebRequest -UseDefaultCredentials -Uri "https://maxis-service-prod.domain.com/users/whoami" -SessionVariable var1

    #$response = Invoke-RestMethod -UseDefaultCredentials -WebSession $var1 -Uri https://maxis-service-prod.domain.com/jobs -Method Post -Body $json -ContentType "application/json"

    this fixed the issue.  I'll make another post on the next step I am poking at.
    Monday, December 11, 2017 5:00 PM
  • Of course this has nothing to do with anything in your original post.

    \_(ツ)_/

    Monday, December 11, 2017 6:06 PM
  • 
    $guid = [guid]::NewGuid()
    
    $json =  [ordered]@{
    schema = "com.domain.maxis.job.export:2014-05-01"
    type = "documentSearch"
    authorizations = @([ordered] @{
    id = "$guid"
    effect = "ALLOW"
    action = "CAN_READ"
    principal = "kerberos:USER@DOMAIN.COM"
    resource = "this"
    })
    parameters = [ordered] @{
    search = [ordered] @{
    schemaType = "com.domain.maxis.issue"
    q = "status:Open and containingFolder:ab2b0635-5021-4551-a050-576e"
    sort = "lastUpdatedConversationDate desc"
    }
    exportData = [ordered] @{
    type = "table"
    columns = @(
    @{columnId = "issueid"},
    @{columnId = "title"},
    @{columnId ="status"},
    @{columnId = "nextstepaction"},
    @{columnId = "assignedfolderlabel"},
    @{columnId = "labels"},
    @{columnId = "requesteridentity"},
    @{columnId = "createdate"},
    @{columnId = "lastupdateddate"},
    @{columnId = "lastresolveddate"},
    @{columnId = "assigneeidentity"},
    @{columnId = "priority"},
    @{columnId = "rank"},
    @{columnId = "planningestimate"},
    @{columnId = "estimatedstartdate"},
    @{columnId = "estimatedcompletiondate"},
    @{columnId = "actualstartdate"},
    @{columnId = "actualcompletiondate"},
    @{columnId = "needbydate"},
    @{columnId = "issueurl"},
    @{columnId = "submitteridentity"},
    @{columnId = "resolvedbyidentity"},
    [ordered] @{path = "/customFields/string/customer/value"
    outputFieldName="Customer%20(string)"},
    [ordered] @{path = "/customFields/string/tshirt_size/value"
    outputFieldName="Tshirt%20Size%20(string)"})
    }
    exportFiles = @([ordered] @{
    id="csv"
    format="csv"}
    )
    exportContent = @{
    type="issues"
    }
        }
    } | ConvertTo-Json -Depth 6
    
    
    # Get a SIM Session Cookie
    Invoke-WebRequest -UseDefaultCredentials -Uri "https://maxis-service-prod.domain.com/users/whoami" -SessionVariable sim
    
    
    $response = Invoke-RestMethod -UseDefaultCredentials -WebSession $sim -Uri https://maxis-service-prod.domain.com/jobs -Method Post -Body $json -ContentType "application/json"
    
    $documentID = $response.id
    $checkstatus = $response.status
    
    if ($checkstatus.status -ne 'Completed') {
    
        do {
    
            $checkstatus = Invoke-RestMethod -UseDefaultCredentials -WebSession $sim -Uri https://maxis-service-prod.domain.com/jobs/$documentID
    
        } until($checkstatus.status = 'Completed')
    }
    
    $completedDoc = Invoke-RestMethod -UseDefaultCredentials -WebSession $sim -Uri https://maxis-service-prod.domain.com/jobs/$documentID
    
    Write-Host $completedDoc
    
    $attachement = $completedDoc.attachment
    
    $savefileTarget = '\\domain.com\dept\dcgsi\Extracts\extract.csv'
    
    Write-Host $attachement
    
    #Invoke-WebRequest -UseDefaultCredentials -WebSession $sim -Uri https://maxis-file-service-prod.proxy.domain.com/jobs/$documentID/attachments/$attachment -OutFile $savefileTarget


    So what this script needs to do is submit the post json; that posted JSON kicks of a job.  That job has an ID ($response.id) and I can then use this id to check the status of the job.  Once it is listed as completed I can then kick over and get the attachment id from the json that is output and mash that up into a final URL to download the document.

    The problem I am having is that the loop part doesn't kick off it just skips it.  The initial response.status is 'scheduled', but then I have to go to a different URL to get the updated status on a get request.  Am I going about this wrong?


    • Edited by J.Pulley Monday, December 11, 2017 9:05 PM
    • Merged by Bill_Stewart Friday, January 26, 2018 3:27 PM Duplicate
    Monday, December 11, 2017 8:41 PM
  • Start by formatting and posting you code correctly using the code posting tool provided.  As posted it is unreadable as code.

    Add trace statements in code or run under debugger to determine what is going wrong.


    \_(ツ)_/


    • Edited by jrv Monday, December 11, 2017 8:55 PM
    Monday, December 11, 2017 8:55 PM
  • Start by formatting and posting you code correctly using the code posting tool provided.  As posted it is unreadable as code.

    Add trace statements in code or run under debugger to determine what is going wrong.


    \_(ツ)_/


    updated to a code block, which looks pretty much the same except it has pretty colors.

    I didn't think the code above was complicated enough that a much more experienced powershell coder couldn't see what was going on.  I'll see about putting in some trace statements soon.
    Monday, December 11, 2017 9:07 PM
  • "formatting" means proper indentation which is what makes it readable as code.


    \_(ツ)_/

    Monday, December 11, 2017 9:09 PM
  • Here is you issue:

    $documentID = $response.id
    $checkstatus = $response.status
    
    if ($checkstatus.status -ne 'Completed') {
    This illustrates the rule against assigning interim variables.  They lead to mistakes and are completely unnecessary.


    \_(ツ)_/


    • Edited by jrv Monday, December 11, 2017 9:11 PM
    Monday, December 11, 2017 9:10 PM
  • Here is you issue:

    $documentID = $response.id
    $checkstatus = $response.status
    
    if ($checkstatus.status -ne 'Completed') {
    This illustrates the rule against assigning interim variables.  They lead to mistakes and are completely unnecessary.


    \_(ツ)_/


    I assigned that because the URL changes from the initial response to what the DO loop has to reference.  I suppose I really don't actually need to do that because I already know that it won't have a response status of completed at that point.  I can just initiate the do loop and let it go.  

    documentID is used in several places and thus is needed to be stored.
    Monday, December 11, 2017 9:15 PM
  • Look more closely at the code.  Use the debugger.  The loop can never be entered.  The "if" will never be true.


    \_(ツ)_/

    Monday, December 11, 2017 9:21 PM
  • Look more closely at the code.  Use the debugger.  The loop can never be entered.  The "if" will never be true.


    \_(ツ)_/

    Yea I removed the IF statement, it didn't actually need to be there.  I already know that at document creation there will be no status of completed. I pretty sure I am stuck on getting the attachment ID.  It should be in the json that happens as part of the do loop; I probably need to return it, but the thing I am thinking I am stuck on is that the ID is attachements >0>id (so three levels deep in the json).  I think I am not doing that retrieval right.

    $guid = [guid]::NewGuid()
    
    $json =  [ordered]@{
    schema = "com.DOMAIN.maxis.job.export:2014-05-01"
    type = "documentSearch"
    authorizations = @([ordered] @{
    id = "$guid"
    effect = "ALLOW"
    action = "CAN_READ"
    principal = "kerberos:USER@DOMAIN.COM"
    resource = "this"
    })
    parameters = [ordered] @{
    search = [ordered] @{
    schemaType = "com.DOMAIN.maxis.issue"
    q = "status:Open and containingFolder:ab2b0635-5021-4551-a050-576e7901ad06"
    sort = "lastUpdatedConversationDate desc"
    }
    exportData = [ordered] @{
    type = "table"
    columns = @(
    @{columnId = "issueid"},
    @{columnId = "title"},
    @{columnId ="status"},
    @{columnId = "nextstepaction"},
    @{columnId = "assignedfolderlabel"},
    @{columnId = "labels"},
    @{columnId = "requesteridentity"},
    @{columnId = "createdate"},
    @{columnId = "lastupdateddate"},
    @{columnId = "lastresolveddate"},
    @{columnId = "assigneeidentity"},
    @{columnId = "priority"},
    @{columnId = "rank"},
    @{columnId = "planningestimate"},
    @{columnId = "estimatedstartdate"},
    @{columnId = "estimatedcompletiondate"},
    @{columnId = "actualstartdate"},
    @{columnId = "actualcompletiondate"},
    @{columnId = "needbydate"},
    @{columnId = "issueurl"},
    @{columnId = "submitteridentity"},
    @{columnId = "resolvedbyidentity"},
    [ordered] @{path = "/customFields/string/customer/value"
    outputFieldName="Customer%20(string)"},
    [ordered] @{path = "/customFields/string/tshirt_size/value"
    outputFieldName="Tshirt%20Size%20(string)"})
    }
    exportFiles = @([ordered] @{
    id="csv"
    format="csv"}
    )
    exportContent = @{
    type="issues"
    }
        }
    } | ConvertTo-Json -Depth 6
    
    
    # Get a SIM Session Cookie
    Invoke-WebRequest -UseDefaultCredentials -Uri "https://maxis-service-prod.domain.com/users/whoami" -SessionVariable sim
    
    #Send Post to Create Job
    $response = Invoke-RestMethod -UseDefaultCredentials -WebSession $sim -Uri https://maxis-service-prod.domain.com/jobs -Method Post -Body $json -ContentType "application/json"
    
    #store the document ID for job retrieval
    $documentID = $response.id
    
    #Loop until the job is completed
    do {
            $checkstatus = Invoke-RestMethod -UseDefaultCredentials -WebSession $sim -Uri https://maxis-service-prod.domain.com/jobs/$documentID
            Start-Sleep -s 10
    
    } until($checkstatus.status = 'Completed')
    
    #get attachments json section
    $attachment = $checkstatus.attachments
    
    #get ID for the attachment
    $attachmentID = $attachment.id
    
    #set the save file location
    $savefileTarget = '\\DOMAIN.com\dept\dcgsi\Extracts\SIM_TechComm_Issues.csv'
    
    #Get the csv file
    Invoke-WebRequest -UseDefaultCredentials -WebSession $sim -Uri https://maxis-service-prod.domain.com/jobs/$documentID/attachments/$attachmentID -OutFile $savefileTarget

    Monday, December 11, 2017 9:48 PM
  • Post the response XML.


    \_(ツ)_/

    Monday, December 11, 2017 9:53 PM
  • Post the response XML.


    \_(ツ)_/

    @{schema=com.domain.maxis.job.export:2014-05-01; type=documentSearch; authorizations=System.Object[]; parameters=; id=49bbf471-da00-48a7-90cd-a12f923c35d0; deleted=False; status=Scheduled; output=; activityData=; attachments=System.Object[]; requesterIdentity=kerberos:USER@DOMAIN.COM; createDate=2017-12-11T22:07:16.465Z; lastUpdatedDate=2017-12-11T22:07:16.465Z; submitterIdentity=kerberos:USER@DOMAIN.COM; editCount=
    1}
    The strange part is that I am seeing System.Object[] where the json has sub elements.  I am not exactly sure why.
    Monday, December 11, 2017 10:09 PM
  • I might even be wrong about the loop actually working.  It worked once and I moved on; I am not sure it is working now.
    • Edited by J.Pulley Monday, December 11, 2017 10:16 PM
    Monday, December 11, 2017 10:15 PM
  • I might even be wrong about the loop actually working.  It worked once and I moved on; I am not sure it is working now.
    Yea the loop isn't actually doing what I am expecting.
    Monday, December 11, 2017 10:21 PM
  • You are not authenticating.


    \_(ツ)_/

    Monday, December 11, 2017 10:32 PM
  • I might even be wrong about the loop actually working.  It worked once and I moved on; I am not sure it is working now.

    Yea the loop isn't actually doing what I am expecting.

    Can I just tell you how maddening a single letter typo can be?  everything that wasn't working was because of "Completed" in the until part of the do loop.  "Complete" is what it needed to be.

    appreciate you letting me bounce this off you.

    Monday, December 11, 2017 10:39 PM