unosquare / embedio

A tiny, cross-platform, module based web server for .NET
http://unosquare.github.io/embedio
Other
1.46k stars 176 forks source link

how to use WebApiConroller and Post. #526

Closed Novotnde closed 3 years ago

Novotnde commented 3 years ago

Hello,

I would kindly ask for explanation of the WebApiConroller and Post.

I am trying to post something, I have tried get but post seems to be better for what I need. I have created only simple sample to return some numbers

  [Route(HttpVerbs.Post, "/test/")]
        public int PostFile()
        {

            return 25852;
        }

And then use client.PostAsync, but client.PostAsync needs two parameters. Url and Content, but the method that return numbers doest get called at all. I am sorry to bother but I can't find any solution even in your cookbook, all examples are of course clear but if you are beginner like me then don't say much.

private async Task TestPost()
        {
            try
            {
                var handler = new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                };
                using (var client = new HttpClient(handler))
                {
                    client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("*"));

                    using (var response = await client.PostAsync($"{DefaultUrl}test/", CONTENT ?? ).ConfigureAwait(false)) // calling controller?
                    {
                        var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                        Result += "Result = " + (string.IsNullOrEmpty(responseString) ? "<Empty>" : responseString);

                    }
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

MY server

 Task.Factory.StartNew(async () =>
        {
            using (var server = new WebServer(HttpListenerMode.EmbedIO, "http://*:8089"))
            {
                Assembly assembly = typeof(App).Assembly;
                server.WithLocalSessionManager();
                server.WithWebApi("/api", m => m.WithController(() => new PdfSourceController()));
                server.WithEmbeddedResources("/", assembly, "Reader.html");
                await server.RunAsync();
            }
        });
rdeago commented 3 years ago

Hello @Novotnde, nice to see you're still using EmbedIO.

A complete explanation of HttpClient is way beyond me, but I can tell you that the content parameter must be an instance of a class derived from HttpContent. Follow the link for the official MS documentation.

As for server-side code, before we even start it would be very helpful to see your Web server initialization code.

Novotnde commented 3 years ago

No I don't need that , I just I am not sure ow to actually ise the Web Api call and its content in the method. All examples I found had the content right above the client.post so I am just unsure if it will work without he controller?

rdeago commented 3 years ago

Well, for a quick test you can use the simplest possible HttpContent: new ByteArrayContent(new byte[0]).

Novotnde commented 3 years ago

thank you, this way I get 404 - Not found, so it does not see the [Route(HttpVerbs.Post, "/test/")]

 public int PostFile()
        {
            return 25852;
        }
rdeago commented 3 years ago

Are you sure your client is requesting the correct path, i.e. /api/test?

Novotnde commented 3 years ago

so sorry, I have been trying to make this work for several hours and decided to do this simple sample and even forgot to add the correct path . I am trying to post pdf

rdeago commented 3 years ago

This section of the wiki deals specifically with file uploads. You'll have to do some googling for the client-side code, it shouldn't be hard to find. 😉

Novotnde commented 3 years ago

yes, have seen that. thank you