Welcome Forums General PowerShell Q&A Invoke-RestMethod

This topic contains 1 reply, has 2 voices, and was last updated by

1 year, 9 months ago.

  • Author
  • #72331

    Points: 26
    Rank: Member


    I'm trying to get my head around the Invoke-Restmethod, but I'm getting lost so hoping someone can help me out.

    To start off I'm trying to post a new post to my WordPress site. I'll start off my using:

    $R = Invoke-Webrequest

    This gets the login form for which i can then create an array to pass the creds into the login form:

    $R = Invoke-WebRequest
    $R.Forms[0].Fields.user_login = "username"
    $R.Forms[0].Fields.user_pass = 'password'
    $Session = Invoke-WebRequest -Method POST -Body $R

    So now i think i have logged in, but im not 100% sure?

    I now want to create the new post so i, so i think i do this.....

    $params = @{ 
        title = "test Rest API post" 
        content = "test Rest API post content" 
        status = 'publish' 
    Invoke-RestMethod -Method post -Uri -ContentType "application/json" -Body $params1 -UseBasicParsing 

    But i get "message":"Sorry, you are not allowed to create posts as this user"

    So I'm a bit lost to how i pass the authenticated session into the restmethod. I've followed some examples and played around with the facebook example, but it's does tie up with this.

    Any help would be really appreciated.



  • #72406

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    The web server doesn't "remember" who you are; that's you job, based on the cookie that you got back from the login form. You have to pass that information along in each request.

    Now, I don't know much about WordPress' JSON API, but I suspect it isn't cookie-based – most REST APIs aren't. So you need to figure out how that API wants you to identify yourself. You're not doing anything wrong from a PowerShell perspective; you're just not doing whatever WordPress wants you to do. Sadly, this is probably not a great place to get WordPress API questions answered accurately ;).

The topic ‘Invoke-RestMethod’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort