How to log a post into a web api

Asked

Viewed 358 times

1

I want to know what the Web Api is receiving in the Post that I am sending.

This is the code I’m using.

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://d6dc30b8-0ee0-4-231-b9ee.azurewebsites.net/");
                    httpWebRequest.Method = "POST";
                    httpWebRequest.ContentType = "application/json";
                    httpWebRequest.Accept = "application/vnd.lyoness.servicesv1 + json";
                    httpWebRequest.Headers.Add("Date" + tempo); 

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();

                }
                MessageBox.Show(json);

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var teste = streamReader.ReadToEnd();
                    MessageBox.Show(teste);
                }

1 answer

1


I recommend using Fluent HTTP for this. In addition to being more elegant to perform HTTP requests, comes several other features like the one you need:

FlurlHttp.Configure(c => {
    c.BeforeCallAsync = DoSomethingBeforeCallAsync;
    c.AfterCallAsync = DoSomethingAfterCallAsync;
    c.OnErrorAsync = HandleErrorAsync;
});

To make a POST:

await "http://api.foo.com".PostJsonAsync(new { a = 1, b = 2 });

Source: http://tmenier.github.io/Flurl/configuration/

  • how can I use Fluent HTTP with my code ?

  • Just follow the documentation, it’s simple to understand. See here http://tmenier.github.io/Flurl/fluent-http/

  • How can I download this ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.