locked
Post with JObject Parameter RRS feed

  • Question

  • User-839182035 posted

    Hi all.

    Please, I need help. I need to call a Post method defined in a Controller, which has a JObject Parameter.

    I'm getting a 500 Error code When I call this method with PostAsJsonAsync or PostAsync.

    The code below shows how I'm calling this Post Method:

    using (var client = new HttpClient())
    {
    client.BaseAddress = new Uri("http://localhost:54810");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var jsonObject = new JObject();
    dynamic json = jsonObject;
    json.Add("mytoken", "teste");

    json.eventos = new JArray() as dynamic;

    dynamic evento = new JObject();
    evento.ID = 1;
    evento.Latitude = 50;
    evento.Longitude = 20;
    evento.Local = 30;

    json.eventos.Add(evento);

    HttpResponseMessage response = await client.PostAsJsonAsync("api/eventocontrole", jsonObject);

    The response.IsSuccessStatusCode property is returning false.

    If I change this parameter type from JObject to any other custom class (eg. User, Product, Cart), the property IsSuccessStatusCode returns true!

    Am I using JObject incorrectly? Should I not use JObject as parameter?

    Just to clarify, I'm using JObject because I need to pass a list of objects and a token string to validate my request.

    Thanks (Thiago from Brazil)

    Friday, April 10, 2015 3:04 PM

Answers

  • User-839182035 posted

    I could not to use JObject like parameter.

    After many tests, the only solution was to create a custom class

    public class RequestData<T>
    {
    public T data { get; set; }
    public string token { get; set; }
    }

    My test below is calling this server method:

    using (var client = new HttpClient())
    {
    client.BaseAddress = new Uri("http://localhost:54810");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    List<EventoControle> eventos = new List<EventoControle>();

    EventoControle evento = new EventoControle();
    evento.ID = 1;
    evento.Latitude = 50;
    evento.Longitude = 20;
    evento.Local = 30;

    eventos.Add(evento);

    RequestData<List<EventoControle>> rq = new RequestData<List<EventoControle>>();
    rq.data = eventos;
    rq.token = "teste";

    HttpResponseMessage response = await client.PostAsJsonAsync<RequestData<List<EventoControle>>>("api/eventocontrole", rq);
    if (response.IsSuccessStatusCode)
    {
    }
    }


    My server method:

    public void PostEventoControle(RequestData<List<EventoControle>> json)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2015 10:09 AM

All replies

  • User1066278571 posted

    The JObject is not understood on the server side. You can add your token using the SetBearerToken method.

     var client = new HttpClient();
     client.SetBearerToken(token);

    If you want to use JObject to create your request body, use the ToObject<> method to send the HTTP POST. If you want to send a JObject to the server, then this needs to be implemented as a parameter on the server side.

    greetings Damien

    Sunday, April 12, 2015 4:43 AM
  • User-839182035 posted

    I could not to use JObject like parameter.

    After many tests, the only solution was to create a custom class

    public class RequestData<T>
    {
    public T data { get; set; }
    public string token { get; set; }
    }

    My test below is calling this server method:

    using (var client = new HttpClient())
    {
    client.BaseAddress = new Uri("http://localhost:54810");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    List<EventoControle> eventos = new List<EventoControle>();

    EventoControle evento = new EventoControle();
    evento.ID = 1;
    evento.Latitude = 50;
    evento.Longitude = 20;
    evento.Local = 30;

    eventos.Add(evento);

    RequestData<List<EventoControle>> rq = new RequestData<List<EventoControle>>();
    rq.data = eventos;
    rq.token = "teste";

    HttpResponseMessage response = await client.PostAsJsonAsync<RequestData<List<EventoControle>>>("api/eventocontrole", rq);
    if (response.IsSuccessStatusCode)
    {
    }
    }


    My server method:

    public void PostEventoControle(RequestData<List<EventoControle>> json)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2015 10:09 AM