Last active
October 22, 2015 03:19
-
-
Save JamesGreenAU/85e79bdb6ad76733dd91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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