I am trying to replicate a database using .NET classes (httpWebRequest) instead of curl. Unfortunately, the cURL works, but the (assumably) same syntax in .NET fails with a 404 “Unauthorized to access or create database” error. Below is my code.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("sourceURL");
request.Method = "POST";
request.ContentType = "application/json";
long ticks = DateTime.UtcNow.Ticks;
string json = "{ \"_id\":\"database_replicate" + ticks + "\", \"source\":\"sourceURL/database\", \"target\":\"database\", \"create_target\":true, \"user_ctx\": { \"roles\": [\"_admin\"] } }";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(json);
writer.Close();
}
Then it fails right here, on the response:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
I am replicating to and from localhost. I’ve tried passing the credentials using request.Credentials, but that doesn’t change the problem. Neither does passing the credentials explicitly in the url.
As it turns out, the problem was that my username and password weren’t being caught. Whether I tried passing them in as part of the url, or as a credential attribute, it wasn’t picking them up. I had to create an authorize header like this
to make it work.