How do I pass along json patch data with rest client in asp.net?

758 Views Asked by At

We use a rest api to get customer information. A lot of the GET request were already written by others. I was able to follow their code to create other GET request, but one of the API methods for updating a customer requires using json patch. Below I have pasted in sample code of a current GET method, a Patch method (that I don't know how to implement) and a sample function written in javascript on how to use the json-patch that came from the api creators demo documentation:

public GetCustomerResponse GetCustomerInfo(CustomerRequest request)
{
    //All of this works fine the base url and token info is handled elsewhere
    var restRequest = CreateRestRequest($"customer/account?id={request.id}", RestSharp.Method.GET);

    var response = CreateRestClient().Execute<GetCustomerResponse>(restRequest);

    if (response.StatusCode == HttpStatusCode.OK)
    {
        return response.Data;
    }
    else
    {
        return new GetCustomerResponse(response.Content);
    }   
}

public EditCustomerResponse EditCustomer(EditCustomerRequest request)
{
    var restRequest = CreateRestRequest($"customer/account?id={request.id}", RestSharp.Method.PATCH);

    var response = CreateRestClient().Execute<EditCustomerResponse>(restRequest);

    //how do I pass along json patch data in here???
    //sample json might be like:
    //[{'op':'replace','path':'/FirstName','value':'John'},{'op':'replace','path':'/LastName','value':'Doe'}]
    
    if (response.StatusCode == HttpStatusCode.OK)
    {
        return response.Data;
    }
    else
    {
        return new EditCustomerResponse(response.Content);
    }
}


//javascript demo version that is working
function patchCustomer(acctId, patch, callback) {
    var token = GetToken();

    $.ajax({
        method: 'PATCH',
        url: BaseURI + 'customer/account?id=' + acctId,
        data: JSON.stringify(patch),
        timeout: 50000,
        contentType: 'application/json; charset=UTF-8',
        beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token) },
    }).done(function (data) {
        if (typeof callback === 'function')
            callback.call(data);
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log("Request failed: " + textStatus);
        console.error(errorThrown);
        failureDisplay(jqXHR);
    });
}
1

There are 1 best solutions below

0
On BEST ANSWER

This was pretty simple. After viewing similar questions on stackoverflow, I initially was trying something like this:

var body = new
            {
                op = "replace",
                path = "/FirstName",
                value = "John"
            };
            
restRequest.AddParameter("application/json-patch+json", body, ParameterType.RequestBody);

It would not work. To get it to work, I added a patchparameters class with op, path and value properties, and then added a list property of type patchparameters to my EditCustomerRequest class and used it like this:

restRequest.AddJsonBody(request.patchParams);