Tony Pham
Tony Pham

Reputation: 246

Update a record in the table on Azure mobile backend by TableController

I created WorkTableController.cs which is inherited from TableController (for using Offline Sync of Azure).

   // PATCH tables/Work/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task<Work> PatchWork(string id, Delta<Work> patch)
    {
         return UpdateAsync(id, patch);
    }

    // POST tables/Work
    public async Task<IHttpActionResult> PostWork(Work item)
    {
        WorkOrder current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);
    }

I also created AppServiceAccess.cs to invoke api from WorkTableController.cs

.....
client = new RestClient(appServiceUrl);
client.AddDefaultHeader("ZUMO-API-VERSION", "2.0.0");
client.Authenticator = new RestSharp.Authenticators.OAuth2AuthorizationRequestHeaderAuthenticator(result.AccessToken);

public static IRestResponse SendPostRequest(string url, object bodyParameter)
{
    var request = new RestRequest(url, Method.POST);
    request.RequestFormat = DataFormat.Json;
    request.AddBody(bodyParameter);
    return client.Execute(request);
}

public static IRestResponse SendPatchRequest(string url, object bodyParameter)
{
    var request = new RestRequest(url, Method.PATCH);
    request.RequestFormat = DataFormat.Json;
    request.AddBody(bodyParameter);
    return client.Execute(request);
}

I can insert data to table Work in database by invoke SendPostRequest(), no problem. But I cannot update any record in table Work by invoke SendPatchRequest().

Could you give me a solution? Where is the problem in my code? How to update a record in the table on Azure mobile backend by using TableController?

Upvotes: 2

Views: 256

Answers (1)

Bruce Chen
Bruce Chen

Reputation: 18465

The update operation request would look like as follows:

PATCH https://{your-app-name}.azurewebsites.net/tables/{table-name}/{id}
Body: json payload

Based on your code, the code would look like as follows:

var client = new RestClient("https://{your-app-name}.azurewebsites.net");
client.AddDefaultHeader("ZUMO-API-VERSION", "2.0.0");
var request = new RestRequest("tables/Work/81c0ca73-e554-4166-a016-c80591bf5924", Method.PATCH);
request.RequestFormat = DataFormat.Json;
request.AddBody(new
{
    text="hello world"
});
var result= client.Execute(request);

For your SendPatchRequest method, the parameter url need to look like this:

tables/{table-name}/{record-Id}

UPDATE:

You could set config.IncludeErrorDetailPolicy to IncludeErrorDetailPolicy.Always under Startup.MobileApp.cs of your mobile app back-end for retrieving detailed error messages to troubleshoot this issue. I just used postman to simulate the patch operation as follows:

enter image description here

Upvotes: 2

Related Questions