couchdbinvoke-webrequest

Why Does Invoke-WebRequest not authenticate with CouchDB while username and password are supplied?


I'm hosting a local instance of couchdb via docker at localhost:5984/. With a python requests client, I can make admin changes via API like so:

>>> requests.put('http://admin:password@localhost:5984/testdb').json()
{'ok': True}

However, when attempting with Windows HTTP client, couchdb says I'm not an admin user. I'm interpreting this as a failed authentication.

> Invoke-WebRequest -Method PUT http://admin:password@localhost:5984/testdb
Invoke-WebRequest : {"error":"unauthorized","reason":"You are not a server admin."}
At line:1 char:1
+ Invoke-WebRequest -Method PUT http://admin:password@localhost:5984/te ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Does Invoke-WebRequest have some kind of behavior preventing ability to embed credentials within the URL scheme?


Solution

  • Inserting the username and password in the URL as you are doing is not supported by Invoke-WebRequest. Instead, you have two options:

    1. Use Get-Credential to build a username/password credential and then pass it to Invoke-WebRequest using the -Credential parameter. For instance:
    $cred = Get-Credential "admin" # this will prompt you for the password
    Invoke-WebRequest -Credential $cred -Method PUT http://localhost:5984/testdb
    
    1. Base64-encode the 'username:password' string and pass that as a basic authentication header with the -Headers parameter. For instance:
    $auth = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("admin:password"))
    Invoke-WebRequest -Headers @{Authorization = "Basic " + $auth} -Method PUT http://localhost:5984/testdb