Flurl: Request failed with status code 500 (Internal Server Error)

Created on 2 Aug 2017  路  6Comments  路  Source: tmenier/Flurl

Hi! I downloaded the library Flurl.http to do GET and POST requests in my project. I start with the POST method, in which I send a json and receive an int (an id).

public async Task<int> PostTarea(Tarea tarea)
        {
            string url_base = WebConfigurationManager.AppSettings["url_base"];
            string json = JsonConvert.SerializeObject(tarea);
            string url = url_base + "/v1/tareas";
            try
            {
                string responseString = await "http://localhost:61630/v1/tareas"
                .PostJsonAsync(json)
                .ReceiveString();

                Int32.TryParse(responseString, out int id_tarea);

                return id_tarea;
            }
            catch (FlurlHttpException ex)
            {
                var error = ex.GetResponseJson();
                return 0;
            }
       }

When I execute them, I receive this in Swagger :

{
"Message": "Error.",
"ExceptionMessage": "Request to http://localhost:61630/v1/tareas failed with status code 500 (Internal Server Error).",
"ExceptionType": "Flurl.Http.FlurlHttpException",
"StackTrace": " en Flurl.Http.Configuration.FlurlMessageHandler.d__2.MoveNext()\r\n--
/.../
}

Any idea what it could be? Thanks in advance!

Most helpful comment

With Flurl, don't do this:

string json = JsonConvert.SerializeObject(tarea);

Just do PostJsonAsync(tarea) and Fllurl will serialize it for you.

All 6 comments

@Aw3same This is internal error in your API backend with status code 500.

Yep, I know, but if i use this code and the same data all works fine:

public async Task<int> PostTarea(Tarea tarea)
        {
            string url_base = WebConfigurationManager.AppSettings["url_base"];
            string data = "";
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(url_base);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                StringContent content = new StringContent(JsonConvert.SerializeObject(tarea), Encoding.UTF8, "application/json");
                // HTTP POST
                HttpResponseMessage response = await client.PostAsync("/v1/tareas", content);
                if (response.IsSuccessStatusCode)
                {
                     data = await response.Content.ReadAsStringAsync();
                }
            }
           Int32.TryParse(responseString, out int id_tarea);

           return id_tarea;
       }

What is the difference betwen codes? I think that Flurl uses this

With Flurl, don't do this:

string json = JsonConvert.SerializeObject(tarea);

Just do PostJsonAsync(tarea) and Fllurl will serialize it for you.

Doing this:

public async Task<int> PostTarea(Tarea tarea)
        {
            string url_base = WebConfigurationManager.AppSettings["url_base"];
            string url = url_base + "/v1/tareas";
            try
            {
               string responseString = await url.PostJsonAsync(tarea).ReceiveJson();

                Int32.TryParse(responseString, out int id_tarea);

                return id_tarea;
            }
            catch (FlurlHttpException ex)
            {
                dynamic d = ex.GetResponseJson();
                string s = ex.GetResponseString();
                return 0;
            }
}

I receive an exception, an the values of d and s are null

Ah...I think you want ReceiveString(), not ReceiveJson().

Solved! Change .ReceiveJson(); for .ReceiveString(); and now all works! Thank you !

Was this page helpful?
0 / 5 - 0 ratings