0

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.

Fabricio Rodriguez
  • 3,769
  • 11
  • 48
  • 101

1 Answers1

0

You must set the Content-type on the headers, just like so.

client.Headers.Add("Content-Type", "application/json");

This will provide the media type for the endpoint to know what media type it is receiving. If you still get the error, you endpoint might not accept json.

  • Thanks Steffen. I added your suggestion, and now I get a (400) Bad Request error. The JSON patch document was malformed and could not be parsed. Instead of using a raw json string, i've created a "PatchDocument" object (with three fields: op, path and value), and then use JsonConvert.SerializeObject() to serialize, but I still get the 400 error. Not sure what is wrong with my JSON... the endpoint definitely accepts JSON. – Fabricio Rodriguez Feb 12 '20 at 09:22
  • Also your json string is using to many double quotation marks. It should be like this: `string json = @"[{ "op": "replace", "path": "//filename", "value": "somefile.pdf" }]";` – Steffen Nielsen Feb 13 '20 at 11:25