Just made the transition from Sql Server to Microsoft's DocumentDB service, and I am struggling to create a document through their REST API ( https://msdn.microsoft.com/en-us/library/azure/dn803948.aspx ). I've tried with RestSharp and WebClient with no luck. I'm sure it's probably a simple oversight. The following code has an Account object with only the required property (id) ...just for the sake of testing. Any help would be great.
public class Account
{
public string id { get; set; }
}
////////////////////////////////////
Account customerAcc = new Account
{
id = "test1"
};
////////////////////////////////////////////
var baseUrl = Environment.GetEnvironmentVariable("APPSETTING_documentDB_endpoint");
var primary_key = Environment.GetEnvironmentVariable("APPSETTING_documentDB_primary_key");
var client = new RestClient(baseUrl);
var data = "type=master&ver=1.0&sig=" + primary_key;
RestRequest restRequest = new RestRequest(Method.POST);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddHeader("Authorization", data);
restRequest.AddHeader("x-ms-date",now.ToUniversalTime().ToString());
restRequest.AddBody(customerAcc);
IRestResponse irestResponse = client.Execute(restRequest);
///////////////////////////////////// OR WITH SYSTEM.NET.WEBCLIENT
string result = "";
using (var newClient = new System.Net.WebClient())
{
newClient.Headers[System.Net.HttpRequestHeader.ContentType] = "application/json";
newClient.Headers["Authorization"] = data;
newClient.Headers["x-ms-date"] = now.ToUniversalTime().ToString();
result = newClient.UploadString(baseUrl, "POST", "{\"id\":\"test1\"}");
}
The calls are getting authorized, but continue to get a 400 Status Code (BAD REQUEST) response. I've tried serializing the object, and replacing the restRequest.AddBody with restRequest.AddParameter("application/json; charset=utf-8",json,ParameterType.RequestBody). This didn't have any impact.
If you are trying to use Azure DocumentDB in a .NET application - you may find it easier to simply use the .NET SDK .
Assuming you'd really like to get the REST API working... taking a brief glance, I see a few things that need to be changed:
1.) The auth key looks off.
var primary_key = Environment.GetEnvironmentVariable("APPSETTING_documentDB_primary_key");
// ...
var data = "type=master&ver=1.0&sig=" + primary_key;
// ...
restRequest.AddHeader("Authorization", data);
Rather than passing in a master key, you will need to produce a hash signature. You can find documentation on auth here.
2.) The URI that you sending a POST request to looks a bit off.
var baseUrl = Environment.GetEnvironmentVariable("APPSETTING_documentDB_endpoint");
// ...
result = newClient.UploadString(baseUrl, "POST", "{\"id\":\"test1\"}");
You will need to construct the URI based on the resource you are trying to operate on. For example, the URI for creating a document should look something like:
https://{databaseaccount}.documents.azure.com/dbs/{_rid-db}/colls/{_rid-col}/docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.