0

I want to request a POST action from a console application with C# to Web API.

MyCode from Console Client is:

consoleClient = new HttpClient();
consoleClient.BaseAddress = new Uri("http://localhost:40659/home/receiveinstructions");
consoleClient.DefaultRequestHeaders.Accept.Clear();
consoleClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


HttpResponseMessage response = 
await consoleClient.PostAsJsonAsync("http://localhost:40659/home/receiveinstructions", EventFile.eventFileList);
response.EnsureSuccessStatusCode();

return response.Headers.Location;

My code from WebApi is:

public  ActionResult ReceiveInstructions(string instructions)
{
   ApiFile apiFileObj = new ApiFile();
   apiFileObj.ReceiveInstructions(instructions);

   return View();
}

Its simple... my client want to send a string to web API, the controller from WebAPI receive one parameter

ReceiveInstructions(string instructions)

Parameter instructions is string (JSON format) original info is a collection of objects.

When I'm try.. my console basically don't do anything. Do i need to change something?

Thanks a lot!!

Vmax
  • 119
  • 6
  • 5
    Possible duplicate of [How to make HTTP POST web request](https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request) – Broots Waymb Apr 08 '19 at 19:30
  • 1
    Could you please clarify what "basically don't do anything" means? What do you expect it to do? Do you mean the request never arrives to the API and the client doesn't fail? Or is it stuck in the await state? If there is an error code or exception, please paste it to the question. – kataik Apr 08 '19 at 20:09
  • Most likely, your endpoint is being returned as 'Not found'. With most POST/GET you need to append the parameters to your request. Your base address would be http://localhost:40659/home// and your request would be receiveinstructions/{Params}. In your case 'Instructions'. Also check out http://restsharp.org/ – davedno Apr 08 '19 at 20:27
  • whan i say don't do anything...visualize I'm debugging and when the line of request is executed, nothing happens, no error either – Vmax Apr 08 '19 at 20:54
  • Add some loggers to your RecieveInstructions method. Also check out the link by @BrootWaymb – Akin Okegbile Apr 08 '19 at 21:55

0 Answers0