locked
Add a variable in a Invoke-RestMethod RRS feed

  • Question

  • Hello guys I need your help:

    I would like to pass a variable in the $body of my command :

    $name = "test.com."
    $headers = @{
      "X-API-Key" = 'myApiKey'
    }
    $body = '{"rrsets": [ {"name": '+ $name +', "type": "CNAME", "ttl": 600, "changetype": "REPLACE", "records": [ {"content": "things.com.", "disabled": false } ] } ] }'
    Invoke-RestMethod -Method Patch -Uri https://dnsadmin.ovea.com/api.com -Headers $headers -ContentType "application/json" -Body $body 

    As you can see I try to add the $name variable but it's obviously not the good method, I don't find the correct way to do it.

    Does someone have a solution for me ?

    Thanks for your time.


     
    Thursday, June 18, 2020 9:07 AM

All replies

  • quentinalx, you can try something like this
    $body = "{'rrsets': [ {'name': '$name', 'type': 'CNAME', 'ttl': 600, 'changetype': 'REPLACE', 'records': [ {'content': 'things.com.', 'disabled': false } ] } ] }"

    The opinion expressed by me is not an official position of Microsoft


    • Edited by Vector BCO Thursday, June 18, 2020 9:25 AM
    Thursday, June 18, 2020 9:24 AM
  • Thanks fo your answer, But it's not working I have a bad request return; like I had with the previous command :/ 

    Thursday, June 18, 2020 9:50 AM
  • what do you mean bad request?

    do you have working construction without variable?


    The opinion expressed by me is not an official position of Microsoft


    • Edited by Vector BCO Thursday, June 18, 2020 1:08 PM
    Thursday, June 18, 2020 9:55 AM
  • Sorry if I wasn't clear I exactly have this error message:

    Invoke-RestMethod : {"msg": "Bad Request"}
    Au caractère Ligne:6 : 1
    + Invoke-RestMethod -Method Patch -Uri https://dnsadmin.ovea.com/api/v1 ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation : (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


    Thursday, June 18, 2020 10:30 AM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Yang Yang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, July 3, 2020 5:41 AM