I've got a .Net 4.0 WinForms app which uses WebClient (as unfortunately I have not been able to get it to work with HttpWebRequest nor HttpClient, due to it being in .Net 4.0 and not .Net 4.5).
It consumes a Web API. It performs a GET on the API:
using System.Net;
using Newtonsoft.Json;
using (WebClient client = new WebClient())
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)(3072);
client.Headers.Add("someHeader", 123);
string res = client.DownloadString("http://someurl.com/api/someendpoint");
List<SomeObject> someObjects = JsonConvert.DeserializeObject<List<SomeObject>>(res);
}
It also performs a DELETE:
using System.Net;
using (WebClient client = new WebClient())
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)(3072);
client.Headers.Add("someHeader", 123);
client.UploadValues("http://someurl.com/api/someendpoint", "DELETE", new NameValueCollection());
}
I now need to perform a PATCH, but I am pretty clueless as to how to do it. I've searched the internet, and most examples use HttpClient or HttpWebRequest. I haven't found anything that uses WebClient. So I need to send JSON something like this:
{
"op": "replace",
"path": "filename",
"value": "somefile.pdf"
}
As a PATCH verb request to the API. I have tried this:
using System.Net;
using Newtonsoft.Json;
string json = @"[{
""op"": ""replace"",
""path"": ""//filename"",
""value"": ""somefile.pdf""
}]";
var serializeModel = JsonConvert.SerializeObject(json);
using (WebClient client = new WebClient())
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)(3072);
client.Headers.Add("someHeader", 123);
var response = client.UploadString("http://someurl.com/api/someendpoint/123", "PATCH", serializeModel);
}
But I get this exception:
The remote server returned an error: (415) Unsupported Media Type.
(And this Patch API endpoint works well as it is currently in use by an Angular web app)
Any help would be very much appreciated. Thank you.