3

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.

3
  • The problem isn't the client library, it's the request. It doesn't help to change clients when the request remains broken. First try to create a valid HTTP request with a tool like Postman or curl, then try to Why did you try WebClient instead of HttpClient anyway, or DocumentDB's .NET library? Commented Oct 27, 2015 at 15:33
  • Use Fiddler to see what exactly is sent to the service and compare it to the documentation's [request samples]. The docs say that a 400 means that the Json body is malformed but you may find other headers that are missing Commented Oct 27, 2015 at 15:35
  • @Panagiotis - Thanks for the suggestions, I'll try Fiddler. Regarding the code, I just showed both client versions I tried but didn't run them together. I prefer RestSharp but tried WebClient quickly to see if anything changed. I'm not using the .NET library because I only need a few quick calls in a handler file, and unfortunately have had issues with all the async requirements. Commented Oct 27, 2015 at 18:10

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.