Getting response from REST API calls

Welcome Forums General PowerShell Q&A Getting response from REST API calls

Viewing 11 reply threads
  • Author
    Posts
    • #283948
      Participant
      Topics: 10
      Replies: 16
      Points: 139
      Rank: Participant

      Hi there everyone!

      I am trying to make powershell run commands through an external management site (wordpress multi-site-management dashboard), and i cant seem to be able to retrieve the response from the website.

      The foreach loop which is not returning any response (even though it runs the exact same call) is found here:
      https://gist.github.com/jtn-webkoncept/c7efb31b07474e6af7f2b525ec42db97

      Can someone tell me what i’m missing?

      If i run this, i get the write response back by default (running Powershell ISE):

      $pluginpath = “forminator/forminator.php”
      $cosandsec = “consumer_key=ck_df&consumer_secret=cs_8f745gd”
      $siteid =”977″

      $Url = “https://test.dk/wp-json/mainwp/v1/site/site-update-item?site_id=$($siteid)&type=plugin&slug=$($pluginpath)&$($cosandsec)”

      Invoke-RestMethod -Method ‘Put’ -Uri $url

      ———-

      I would greatly appreciate some feedback!

    • #283951
      Participant
      Topics: 10
      Replies: 16
      Points: 139
      Rank: Participant

      The reponse is in JSON format, perhaps this is why my error-catch attempts are failing?

    • #283966
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      My first guess would be this:

      It is referencing a specific file, which would be Get-Item. Also, here is a better way to build the string, imho:

      and getting results from the the for loop, everything should roll up (no +=) …

    • #283978
      Participant
      Topics: 10
      Replies: 16
      Points: 139
      Rank: Participant

      Hi Rob!

      Thank you for your swift response 🙂

      Regarding the $Filelist ( i forgot to mention that ) – It is supposed to take one ID, going on a list in the txt file:
      1232
      6543
      4563
      – and so on

      Each of the ID’s represent a site to have this specific plugin updated, mentioned in $pluginpath.
      So i want it to loop each line inside the file, which seems to work.

      The call itself, also seems to work now. ( I tried to use your replacement for $Url, but for some reason that broke the call, and i actually did get an error message saying the request failed ).

      I just cant seem to figure out, if i make a simple call, without looping, i get the response i want (just printet out in the console), but i cant seem to catch it in this loop 🙁
      When there is a “normal error” it does get reported back to me, if the URL does not match for an example, but when i am supposed to get a “succes response” in JSON format, i just get nothing.

      Maybe i am not supposed to try and get a JSON response with Exception.message?

    • #283981
      Participant
      Topics: 10
      Replies: 16
      Points: 139
      Rank: Participant

      The response error i do get, to clarify, are not from any JSON format. These are just 404, 403 etc.

    • #283987
      Participant
      Topics: 10
      Replies: 16
      Points: 139
      Rank: Participant

      If i launch this code in a seperate file:

      $pluginpath = "forminator/forminator.php"
      $cosandsec = "consumer_key=ck_5939575f46dd2cb401b1183e8b8&consumer_secret=cs_8f7452d67154e056"
      $siteid ="977"

      $Url = "https://test.dk/wp-json/mainwp/v1/site/site-update-item?site_id=$($siteid)&type=plugin&slug=$($pluginpath)&$($cosandsec)"

      Invoke-RestMethod -Method 'Put' -Uri $url

      write-host $url

       

      I get this response:

      https://test.dk/wp-json/mainwp/v1/site/site-update-item?site_id=977&type=plugin&slug=forminator/forminator.php&consumer_key=ck_52f401b1183e8b8&consumer_secret=cs_8f7452

      SUCCESS
      -------
      Process ran.

      And its the same thing i’m doing in the loop, pretty much.

    • #284020
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      Just to simplify things, look at this structure. First you need to have ErrorAction set to capture errors from Invoke-RunBook. Next, in the try block, you are going to parse $updateprocess, not the exception:

    • #284203
      Participant
      Topics: 10
      Replies: 16
      Points: 139
      Rank: Participant

      Hi again Rob!

      Thank you so much for you details!

      It works perfectly now 🙂

      This is the final structure:
      https://gist.github.com/jtn-webkoncept/5fa2b0f1b8fde93932006491c89352da

      My CSV output now looks like this:

      “websiteid”,”Exceptionmessage”,”Exceptionresponse”,”StatusdescriptionValue”,”Statusdescription”
      “977”,”Process ran.”,,”200″,”SUCCESS”

      Have a fantastic day 🙂

    • #284206
      Participant
      Topics: 10
      Replies: 16
      Points: 139
      Rank: Participant

      As a sidenote, i could not make the script work with your suggested section:

      $Url = https://test.dk/wp-json/mainwp/v1/site/site-update-item?site_id={0}&type=plugin&slug={1}&{2}” -f $websiteid, $pluginpath, $cosandsec

      – so i just the one from the prior version which seems to do the trick 🙂

    • #284257
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      That is just a string format. The curly brackets represent array position for the passed arguments:

      https://test.dk/wp-json/mainwp/v1/site/site-update-item?site_id=1234&type=plugin&slug=myPluginPath&myCosAndSec

      Personal preference over concatenating by putting variables or $($var) in a string.

    • #284350
      Participant
      Topics: 10
      Replies: 16
      Points: 139
      Rank: Participant

      Ah, i see 🙂

      I have tested it a couple of times now this script, and it works, however i was hoping i could ask for some input on the next issue i am fighting aswell.

      The script runs, but it runs very slowly, as it awaits response before initiating the next foreach loop.
      I have tried using PoshRSJob and ForEach-Object (which i have never used before) to allow the function to loop in parallel, testing with 5 parallel processes.

      I keep getting “Parameter set cannot be resolved using the specified named parameters. ” just filling up the CSV output sheet.
      I have looked at alot of examples and syntax but i cant figure out how to get this error fixed.
      Im guessing it have something to do with do with the $Targets not being readable in this manner?
      $Targets is just supposed to be the ID’s used already.

      As far as i can read up on there is no available parallel function simply for “foreach” which is why i am including ForEach-Object and Posh, or atleast trying.

      Here is the modified code:
      https://gist.github.com/jtn-webkoncept/cbff3c178d37ca0fbc40a2b974c4961a

    • #284416
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      Take a look at examples like this:

      https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/write-progress-across-multiple-threads?view=powershell-7.1

      The loop is in the wrong place. You want to process each website in it’s own thread. To emulate the updates taking different time, the WebSite id is used as milliseconds, so 2000 would be 2 seconds.

      This is executed as a job, so you need to understand how to work with jobs. For instance, testing this code a couple of times I have multiple jobs. The results can only be retrieved one time unless you use the -Keep switch.

Viewing 11 reply threads
  • You must be logged in to reply to this topic.