0

From the client in angular.js I am sending this json:

{"variable": "something"}

I am learning to create web services using visual studio c #, and whenever I have tried to send a JSON, I do it in the same way as I am doing, and they are always received (The problem is in the backend, I am sure). When making the web request, the received JSON is always null. How can I solve that?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace pruebaservicioweb.Controllers
{
   public class EstudianteController : ApiController
   {
    [HttpGet]
     public dynamic ReadJSON([FromBody] dynamic  parameter)
     {
        string json = parameter.variable;
        return json;
      }
   }
}
unusuario
  • 139
  • 12
  • 2
    Please add the code that makes the request too :) – Grantly Dec 15 '17 at 20:45
  • 1
    okay... Updating.. – unusuario Dec 15 '17 at 20:46
  • 2
    I don't think web api likes having the `dynamic` keyword in action method signatures. –  Dec 15 '17 at 20:47
  • And also - what about exception handling? Can you log those somewhere? you might (i think definitely) be getting an exception – Grantly Dec 15 '17 at 20:48
  • Get requests (normally) do not have a message body. You should be using POST. – Brandon Miller Dec 15 '17 at 20:51
  • ` private void LogAPIException(Exception ex) { using (EventLog eLog = new EventLog("Application")) { eLog.Source = "Application"; eLog.WriteEntry(ex.ToString(), EventLogEntryType.Error); } } `....Add this function to your catch statement...Its 'gross' but it works :) Sorry I cannot get the formatting better – Grantly Dec 15 '17 at 20:51
  • @BrandonMiller Aha ...Is that what the [FromBody] does? force it to take the object from the Body? Makes sense that GET won't work...GET is great for browser testing but YES I agree - use POST for objects – Grantly Dec 15 '17 at 20:53
  • 2
    Per the spec, GET requests *can* have a message body, but servers are expected to ignore it. –  Dec 15 '17 at 20:55
  • 1
    @Grantly Yeah, that is exactly what it does lol. I agree with you though - I love using GET when testing, but who wants to read JSON from a URL? lol – Brandon Miller Dec 15 '17 at 20:55
  • ...Still waiting for the REQUEST (calling) code... – Grantly Dec 15 '17 at 20:58
  • @Amy, good catch! I honestly didn't know until about 5 minutes ago that GET could even have a body in the request, but apparently CURL supports this functionality? – Brandon Miller Dec 15 '17 at 20:58
  • @Amy - Thanks for the info....Any chance you can post a link of the spec? – Grantly Dec 15 '17 at 21:00
  • @BrandonMiller I can't answer that, I haven't used linux (and cURL) in a few years, but it wouldn't surprise me if it does support GET bodies. [GET with bodies is discussed here](https://stackoverflow.com/questions/978061/http-get-with-request-body) –  Dec 15 '17 at 21:01
  • 1
    @Grantly The answer I linked to in my previous comment has a few links into the spec. –  Dec 15 '17 at 21:01
  • 1
    Wow! You learn something new every day! Thank you Amy (: – Brandon Miller Dec 15 '17 at 21:04
  • Consider using Fiddler4 to monitor your HTTP traffic. Invaluable for this type of development. Are you setting the ContentType to "application/json"? – programmerj Dec 15 '17 at 21:16

0 Answers0