1

I have a C# WebAPI (I'm new to web API) which work fine as I can perform GET & POST request from Postman and from another C# program using HttpClient.

The POST method is "working" since I put a breakpoint into it to check if the code in it was triggered when I send a POST request, it is.

The "another C# program" is in charge of reading some data from a DB, serializing it (with JSON.Net) & sending it to the web API.

In the web API, I want to use the JSON serialized object (which include nested objects in it) to rebuild the same object I had before serializing and sending it. Every class used to create this object exists in the webAPI program.

I think that I just have to receive the JSON string and then deserialize it to my object, but I don't acheive to get this string, instead I get 'null' as the post parameter.

My code :

WebAPI configuration

public static class SHPC_APIConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "ShPcAPI",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional}
        );

        // Configure JSON formatter
        var jsonFormatter = config.Formatters.JsonFormatter;
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }
}

Controller

public class DataController : ApiController
{
    public string Get()
    {
        return "Hello world!";
    }

    public HttpResponseMessage Post([FromBody] string value)
    {
        var data = JsonConvert.DeserializeObject<Data>(value);
        Console.WriteLine(value != string.Empty ? "OK" : "KO");
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
}

Performing a GET request works, I get the answer that I expect. Wherever I'm requesting from Postman or from my other C# program.

However, when I try to perform a POST request, the parameter value of the request is always null, wherever I'm requesting from Postman or from my other C# program.

2
  • Just use the correct type as a parameter. The JSON deserialisation should happen as part of the framework. Also, use WebApi 2 if possible, it's much nicer to define the routes using the attribute approach rather than registering them in config. Commented Aug 8, 2017 at 15:58
  • @DarrenYoung when you say the correct type, are you talking about the type "Data" that I want to deserialized the JSON to ? If it is, I've already try to expect Data as parameter of the Post() method instead of string, but it set a Data object with all default/null values... Commented Aug 8, 2017 at 16:02

1 Answer 1

2

In your WebApiConfig file into App_Start try to put this in the Register function:

var settings = 
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;

This will serialize all the responses in json.

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

3 Comments

Thank you very much that works perfectly ! Do you know why my jsonFormatter var in the Register function doesn't do the job ?
it is because it maybe is trying to return a json into a XML response
Ok, thank you very much for your answer I was stuck in this for hours.

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.