Skip to content

Instantly share code, notes, and snippets.

@JamesGreenAU
Last active October 22, 2015 03:19
Show Gist options
  • Save JamesGreenAU/85e79bdb6ad76733dd91 to your computer and use it in GitHub Desktop.
Save JamesGreenAU/85e79bdb6ad76733dd91 to your computer and use it in GitHub Desktop.
public async Task<R> Put<T, R>(T docObject)
{
string DbName = "demodb";
string NewDocId = "newDocId";
// Valid paths, methods and response codes are documented here: http://docs.couchdb.org/en/stable/http-api.html
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("http://server:5984/");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// NB: PutAsJsonAsync is an extension method defined in System.Net.Http.Formatting
var response = await httpClient.PutAsJsonAsync(DbName + "/" + NewDocId, docObject);
R returnValue;
if (response.StatusCode == HttpStatusCode.OK)
{
returnValue = await response.Content.ReadAsAsync<R>();
}
// Check the docs for response codes returned by each method.
// Do not forget to check for HttpStatusCode.Conflict (HTTP 409) and respond accordingly.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment