0

I've developed a WebApi Put Method :

 [Route("api/woo/setOrderStatusComplete")]
    [HttpPut]
    public HttpResponseMessage SetOrderStatus(string country, string orderNo)

which expects to be called in the form:

http://<myWebAddress>/WooService/api/woo/setOrderStatus?country=uk&orderNo=23662

I've tested my endpoint from swagger and all works fine, However, I'm having difficulty writing a code sample to call my endpoint from other C# Apps that will call this end point. Can anyone point me to an example.

As a starter for 10 I'm using :

HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "PUT";
webrequest.ContentType = "application/json";
bilpor
  • 3,467
  • 6
  • 32
  • 77
  • There are lots of ways in c# to make the API request. Which one are you using? That's the only way we can help you figure out which technique for setting the HTTP method is appropriate. – Kirk Woll Mar 14 '22 at 20:41
  • @KirkWoll I've amended the question to include my stat point – bilpor Mar 14 '22 at 20:43
  • Can you elaborate on your *"difficulty"*? Is there an error? Undesired behavior? – Crowcoder Mar 14 '22 at 20:50
  • @Crowcoder theApi itself works I now need to provide an example of calling the 'PUT' method from outside the Api so that other developers know how to call it. – bilpor Mar 14 '22 at 20:56
  • 1
    @bilpor Please do not use WebRequest, WebClient to make API calls since they are marked as obsolete: https://learn.microsoft.com/en-us/dotnet/core/compatibility/networking/6.0/webrequest-deprecated . Please use the `System.Net.Http.HttpClient` class instead. – Rahul Sharma Mar 14 '22 at 21:12
  • @RahulSharma Most of the Apps that will be using this endpoint are .Net 4.7.2. They are a not in a position to upgrade their apps to .NET Core, so I need to provide an example in the earlier stuff – bilpor Mar 14 '22 at 21:18
  • @bilpor Maybe this will change your mind: https://stackoverflow.com/a/23959136/1807452 – Rahul Sharma Mar 14 '22 at 21:20
  • @RahulSharma so for .net 4.7.2, I can tell developers to call my method in the following form: ` private static HttpResponseMessage CallRestPutMethod(string Url) { var httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(Url); var responseTask = httpClient.PutAsync(Url, null); responseTask.Wait(); return responseTask.Result; }` – bilpor Mar 14 '22 at 21:37
  • @bilpor don't create a [new HttpClient for each request](https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) and do not [block async calls](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html), especially in .NET Framework. – Crowcoder Mar 15 '22 at 00:48

0 Answers0