receive requests in web servce rest in c #?

I have a web service api rest project in C # (basic) in VS, I program a POST method that does nothing at the moment, it only validates if information arrives or that I think it does ha, what I want to know is: when I send a Json chain from a client program, to be able to debug the reception of the request sent from my web service.
I want to see how my request arrives.

The web service is made with the templates that come as an example in Visual Studio, which says web API fullRest, add the controllers folder where the POST and GET methods of my web service are located. Basically my project is very basic, everything is programmed by the template, I just added the methods.

here is my code for the POST method in my web service rest:

[Route("api/prueba")]
    [HttpPost]
    public HttpResponseMessage Post([FromBody] datosTarjeta datoTarjeta )
    {
        try
        {
            if (datoTarjeta != null)
            {
                var respuesta = new HttpResponseMessage(HttpStatusCode.OK);

                return respuesta;
            }
        }
        catch (Exception ex)
        {
            var respuesta = new HttpResponseMessage(HttpStatusCode.BadRequest);
            return respuesta;
        }
        var respuesta1 = new HttpResponseMessage(HttpStatusCode.BadRequest);
        return respuesta1;

    }

}

This is the code that sent the Json string to the web service, it is in VB.net, which sends a Json string:
Imports System
Imports System.Net
Imports System.Text
Imports System.Threading
Imports Newtonsoft.Json

Public Async Sub metodoPost()
    Dim t As New datosTarjeta
    t.Ln_Tarj = "PRO1"
    t.Fiid_Tarj = "B062"
    t.Numero_de_tarjeta = "5359430105739184   "

    Try
        Dim url As String

        url = "http://localhost:60973/api/rest"

        Dim cliente As New Http.HttpClient
        Dim JsonData As String = JsonConvert.SerializeObject(t)
        Dim restContenido As New Http.StringContent(JsonData, Encoding.UTF8, "application/json")
        Dim restRespuesta As Http.HttpResponseMessage = Await cliente.PostAsync(url, restContenido)
        Thread.Sleep(200000)
        Console.WriteLine(restRespuesta.StatusCode.ToString)

    Catch e As Exception
        Dim mensaje As String

        mensaje = e.Message

    End Try


End Sub

If you could help me to know at what point the request reaches the web service or how I can program a method for the paticion to arrive at the web service and be able to see how the request arrives.
Thank you.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

That request probably won’t arrive, because you told the server the route is api/prueba:

[Route("api/prueba")]
[HttpPost]
public HttpResponseMessage Post([FromBody] datosTarjeta datoTarjeta )

And in the client you posted to api/rest
url = "http://localhost:60973/api/rest"

First off, I recommend that you don’t try to write the client and the server. Just write the server, and use Postman to poke it with data, check it works.. then write the client. Battling your own client bugs as well as trying to get its config right is a headache you don’t need. Postman is reliable and a good tool for sending JSON to RESTful webserviecs like you have here. Set a breakpoint in the top of the Post method (call it something else, like.. what it does: UpdateUser, CreateOrder, ChangeShippingAddress etc) then put the url and data into postman and hit Go; your server code should break and you can step through it

Then you can write your client; put the same url and data in, hit Go..


You could probably make your life somewhat easier by adding something like Swashbuckle, Swagger-Net or NSwag package to your server, and then using the Add REST Client feature in VS, or the AutoRest tool (or, apparently NSwag generates client side code too)..

All this “creating a client by crafting an HTTP request by hand” – boring, repetitive etc; you didn’t “receive and parse the HTTP request by hand” on the server end, so why not have the technology write the client side for you too; swagger describes the service in a standard way, that means another tool can consume the description and write client side code that has nicely named methods etc

It’s like the days of old with WCF/ASMX/SOAP/XML/”Add Service Reference” (except in those days it was easier and more reliable)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x