1

I have this Error

The Json value could not be converted to System.Collections.Genereic.List 1[Blazor_fresh_project.Shared.Feedback]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

when I'm trying to CreateObj in this method :

public async Task<List<Feedback>> CreateFeedback(Feedback feedback)
        {
             var resulte = await _httpClient.PostAsJsonAsync($"api/feedback",feedback);
            string content = Newtonsoft.Json.JsonConvert.SerializeObject(resulte);
            Console.WriteLine("Here is THE CONTENT " +content);
             var   feedbacks = await resulte.Content.ReadFromJsonAsync<List<Feedback>>();
            return feedbacks;
        }

This error happen at line :

 var   feedbacks = await resulte.Content.ReadFromJsonAsync<List<Feedback>>();

The console write : Console debug
Do you have any suggestions or tips for debugging this kind of error?

Edit : api/feedback request

namespace Blazor_fresh_project.Server.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    public class FeedbackController : ControllerBase
    {
        List<Feedback> Feedbacks = new List<Feedback> {
           new Feedback{Id=1,Name="arthur",Description="I think this is great",DateOfPost=DateTime.Now,Power=10},
           new Feedback{Id=2,Name="Mario",Description=" coming soon",DateOfPost=DateTime.Now,Power=8},
           new Feedback{Id=3,Name="Chipolo",Description="I think this is great",DateOfPost=DateTime.Now,Power=17},
           new Feedback{Id=4,Name="Coco",Description=" coming soon",DateOfPost=DateTime.Now,Power=12},
           new Feedback{Id=5,Name="Marguerite",Description="I think this is great",DateOfPost=DateTime.Now,Power=6},
           new Feedback{Id=6,Name="Carine",Description=" coming soon",DateOfPost=DateTime.Now,Power=4}
            };
        [HttpGet]
        public async Task<IActionResult> GetFeedbacks()
        {
            return Ok(Feedbacks);
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetSingleFeedback(int id)
        {
            var feedback = Feedbacks.FirstOrDefault(f => f.Id == id);
            if (feedback == null)
                return NotFound("this feedback wasn't found");
            
            return Ok(feedback);
        }
        [HttpPost]
        public async Task<IActionResult> CreateFeedback(Feedback feedback)
        {
            feedback.Id = Feedbacks.Max(h => h.Id + 1);
            Feedbacks.Add(feedback);
            return Ok(feedback);
        }
    }
}


namespace Blazor_fresh_project.Client.Services
{
    public class FeedbackService : IFeedbackService
    {
        private HttpClient _httpClient;
        public FeedbackService(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }
        [Authorize]
        public async Task<List<Feedback>> GetFeedbacks()
        {
          return await _httpClient.GetFromJsonAsync<List<Feedback>>("api/feedback");

        }

        public async Task<Feedback> GetFeedback(int id)
        {
            return await _httpClient.GetFromJsonAsync<Feedback>($"api/feedback/{id}");
        }
        [Authorize]
        public async Task<List<Feedback>> CreateFeedback(Feedback feedback)
        {
             var resulte = await _httpClient.PostAsJsonAsync($"api/feedback",feedback);
            string content = Newtonsoft.Json.JsonConvert.SerializeObject(resulte);
            Console.WriteLine("Here is THE CONTENT " +content);
             var   feedbacks = await resulte.Content.ReadFromJsonAsync<List<Feedback>>();
            return  feedbacks;
        }
    }
}
5
  • The first line is where the problem is. You are making a post to 'api/feedback'. Are you sure it's meant to return a list of feedbacks? Commented Jul 29, 2021 at 10:56
  • thank you for your reply @Qudus Yes I do, the answer of request "localhost:44392/api/feedback" return all my "Feedback",(I added a screen of this) Commented Jul 29, 2021 at 11:23
  • The string in the Console screenshot does not match that of the request. One has [], the other not. You are not getting the same API results. Commented Jul 29, 2021 at 12:24
  • Alright @HenkHolterman, but I can still use the getALL method in this way? Can you clarify this for me? I added another screen to the post method. Thank you for help. Commented Jul 29, 2021 at 13:12
  • The rule on this site is not to post (too many) screenshots of code or errors. Add them as text. Commented Jul 29, 2021 at 13:17

1 Answer 1

1

The 'Console' screenshot shows us a payload of a single Feedback item, the request-in-a-browser shows a list of Feedbacks.

Your code is doing a POST, the browser does a GET. Apparently you have an endpoint with different return types for GET and POST.

Post the (relevant) code of your controller if you need more help.

OK, so indeed you have. You can change your controller or what looks more logical, your client:

    public async Task<Feedback> CreateFeedback(Feedback feedback)
    {
      var resulte = await _httpClient.PostAsJsonAsync($"api/feedback",feedback);
      var feedback = await resulte.Content.ReadFromJsonAsync<Feedback>();
      return feedback;
    }

and adapt the calling code accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay ! I finally got this, this is clear now. Thank you for your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.