0

I am trying to figure out a way to send a Json formatted data to a web api controller in a neat(more natural) way.

let me explain. Suppose I have this controller:

[HttpPost]
public class StudentController : ApiController
{

    public void PostSomething([FromBody] string name, [FromBody] Student s) 
    {
        //do something
    }
}

The json data that I WANT to post is something like this (as it is correctly formatted):

{
    "name" : "John",
    "student" : {
        "id" : "1",
        "age" : "22"
    }
}

But what I SHOULD send for the web api to parameter bind the objects should be like this:

{
    "John",
    {
        "id" : "1",
        "age" : "22"
    }
}

The problem is that if I use my desired json format, both name and student objects will be null in the PostSomething method of the controller.

How can I send a json request with a format similar to the first example to my web api controller?

6
  • The second example doesn't look like valid json to me, and fails on every validator I have tested it with. Commented Jan 20, 2017 at 9:03
  • That is exactly the case and you are totally right. But the web api controller only accepta the parameters if I send them this way. That is the problem Commented Jan 20, 2017 at 11:21
  • Your question isn't entirely clear to me. Is that the JSON you need to receive from a 3rd party, or is that the JSON format you need to create in order to send to a 3rd party? Commented Jan 20, 2017 at 13:04
  • The first example is how i WANT to send the json request, the web api controller won't accept it. The second example is how web api accepts the json(and I dont like it to be this way) Commented Jan 20, 2017 at 15:28
  • 1
    Then you need to write your own descendant of the ParameterBindingAttribute and use that instead of [FromBody] to decorate your parameter. Problem is you will also have to parse this string yourself because it doesn't adhere to any standard - that's going to be a real pain. Can't you get the user to send a properly formatted request? Commented Jan 20, 2017 at 16:02

2 Answers 2

1

In order to consume the desired JSON structure you can change the method signature of the PostSomething and introduce a class that represents the sent data. E.g.

public class StudentTransferObject {
    public string Name {get; set;}
    public Student Student {get; set;}
}

With the Controller:

[HttpPost]
public class StudentController : ApiController
{    
    public void PostSomething([FromBody] StudentTransferObject studentInformation) 
    {
        //do something
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Still I cannot specify the name of the parameter as the key
actually I was wrong and your solution totally works. Thank you
1
  1. Read text from response body and parse the objects yourself:

    [HttpPost] public async Task<string> PostSomething() { string result = await Request.Content.ReadAsStringAsync(); //parse here how you want return result; }

  2. Dynamic serialization with custom binding or JToken.

Comments

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.