Share via

How to Update Fields Using HttpClient

Khidir Elsanosi 21 Reputation points
2021-01-26T09:19:15.167+00:00

I am trying to update few fields on a system using httpClient. The code inside the method is:

HttpClient client = new HttpClient();

// building the uri
string url = "my url";
var uri = new Uri(url);
var uriWithCred = new UriBuilder(uri)
{
UserName = "UserName",
Password = "Password"
}.Uri;

// Prepare the payload, with the field to be updated
HttpContent payload = new StringContent("{\"incident_state\":\"8\"}", Encoding.UTF8, "application/json");

//Update request
var method = new HttpMethod("PATCH");

var request = new HttpRequestMessage(method, uriWithCred)
{
Content = payload
};

var response = await client.SendAsync(request);

if (response.IsSuccessStatusCode)
{
MessageBox.Show("success");
}

it is executing successfully, but somehow I don't get the success status code, and the target system is not updated. What could be wrong about this piece of code?

Thanks

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-01-26T13:46:09.547+00:00

    You need to state what the returned status code is in order to be possibly helped to know what may be the problem with the call to the Web service.

    Was this answer helpful?

    0 comments No comments

  2. Ken Tucker 5,866 Reputation points
    2021-01-26T11:51:00.79+00:00

    You could try a PatchAsync if SendAsync is not working

        var response = await client.PatchAsync(uri, payload);
    

    If you are still having issues I would look at what is going on with the call with a tool like fiddler but you might have to ask the people who run the web service why the patch is not work

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.