2

I have this code below code to create a database in couchDB:

private async void DatabaseCreate()
        {
            if (!await DatabaseExist())
            {
                var contents = new StringContent("", Encoding.UTF8, "text/plain"); 
                this.uri = "http://USER:PASSWORD@localhost:5984/item_sn";
                var response = await client.PutAsync(this.uri, contents); //set the contents to null but same response.
                Console.WriteLine(response.Content.ReadAsStrifngAsync().Result); 
            }
        }

My problem is that it is giving me a response with StatusCode:401, saying "Unauthorized". I tried curling it in the terminal and it gives me a successful response. Is there some preconditions I need to set for the httpclient? Or am I using the wrong method. I know there are some third party package for couchDB but for my case I just want to use C#'s httpclient.

Thanks in advance

curl command:

curl -X PUT http://USER:PASSWORD@localhost:5984/item_sn
  • Can you provide your `curl` command? – Megidd Jun 05 '18 at 13:26
  • Added my curl command to original post. Cheers. – Marcus Aurelius Jun 06 '18 at 00:50
  • If you do a `GetAsync("http://localhost:5984/")` operation, will you get the CouchDB welcome message? Welcome message should be like: `{"couchdb":"Welcome","version":"2.1.1","features":["scheduler"],"vendor":{"name":"The Apache Software Foundation"}}` – Megidd Jun 06 '18 at 01:17
  • Yes. GetAsync(uri) gives me a welcome response from couchdb. Just to add, I have no issues adding/revising documents in an existing database with PutAsync(uri, contents) and PostAsync(uri, contents) operations. My issue is only when creating new database on couchdb. CouchDB version is 2.1.1 if that helps. – Marcus Aurelius Jun 06 '18 at 02:37
  • Is your username/password at CouchDB-level or at database-level? – Megidd Jun 06 '18 at 02:39
  • Hmmm! Not so sure. I just configured it with a single node setup. Is there anything I need to do in the configuration? Would that matter when using c# httpclient? Because curling it seems to work. Cheers – Marcus Aurelius Jun 06 '18 at 02:45
  • Can you create any arbitrary database with the `curl` command? – Megidd Jun 06 '18 at 02:49
  • Yes, with curl command I have no problem creating arbitrary databases. When I run your code above, I get this error: {"error":"not_found","reason":"no such node: couchdb@127.0.0.1"} – Marcus Aurelius Jun 06 '18 at 02:54

2 Answers2

1

Looks like in HttpClient class you can include credentials with HttpClientHandler Class with its Credentials property. Take a look at this answer. Try it, maybe that would work.

Megidd
  • 7,089
  • 6
  • 65
  • 142
  • `var byteArray = Encoding.ASCII.GetBytes("USER:PASS"); client.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(byteArray));` Adding the lines of code before sending the request works. Thanks for the guidance mate. Appreciate it. Cheers – Marcus Aurelius Jun 06 '18 at 04:31
1

Here's the code that worked.

public async void DatabaseCreate()
            {
                if (!await DatabaseExist())
                {
                    var contents = new StringContent("", Encoding.UTF8, "text/plain");
                    var byteArray = Encoding.ASCII.GetBytes("user:pass");
                    client.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(byteArray));
                    this.uri = "http://localhost:5984/databasename";
                    var response = await client.PutAsync(this.uri, contents);
                    Console.WriteLine(response.Content.ReadAsStringAsync().Result); 
                }
            }