2

I am newbie to WEB API2 and JSON overall. I have a JSON body like this

{
 "Input_data": {
 "method": "check",
 "hashcode": " xxxxxx ",
 "accountId": "11111111",
 }
}

How could I retrieve values from POST request?

I have model like this

 [JsonArray]
 public class BaseInput
 { 
 [JsonProperty(PropertyName = "method")]
 public string Method { get; set; }
 [JsonProperty(PropertyName = "hashcode")]
 public string hashCode { get; set; }
 [JsonProperty(PropertyName = "accountid")]
 public int accountId { get; set; }
}

And controller code like this

BaseOutput ApiReqeust(int partnerId,[FromBody] BaseInput Input_data)

And Input_data is always null.

What am I doing wrong?

Nkosi
249k38 gold badges472 silver badges505 bronze badges
asked Apr 20, 2018 at 15:04
5
  • Can you show the URL that you are using to hit the API controller? Commented Apr 20, 2018 at 15:15
  • localhost:51314/api/1/USAPost/ApiRequest Commented Apr 20, 2018 at 15:16
  • 1
    Can you explain which part of the URL aligns to the partnerId value? And maybe post your route config for the method. Commented Apr 20, 2018 at 15:23
  • Why the attribute [JsonArray]? Your model class does not look like an array at all. Not sure if it causes this problem, but even if it is unrelated, I still find it strange. Commented Apr 20, 2018 at 15:24
  • The integer part {1} is allign to the partnerId Commented Apr 20, 2018 at 15:25

2 Answers 2

4

You are using the wrong model for that JSON input

This more matches your JSON model

public class InputData {
 [JsonProperty("method")]
 public string method { get; set; }
 [JsonProperty("hashcode")]
 public string hashcode { get; set; }
 [JsonProperty("accountId")]
 public string accountId { get; set; }
}
public class BaseInput {
 [JsonProperty("Input_data")]
 public InputData Input_data { get; set; }
}

the controller code appears to be fine as it is.

answered Apr 20, 2018 at 15:09

Comments

1

Aside from the matching parameter name problem that @Nkosi mentioned, I believe you have another issue with your JSON parameters aligning with your controller declaraction. When passing a JSON payload to the API controller, the model binding that is built into WEB API will not recognize the BaseInput Input_data as an expected value. To account for this, your JSON should look like:

{
 "method": "check",
 "hashcode": " xxxxxx ",
 "accountId": "11111111"
}

Also, for POST methods, it is best not to use URL variables in your parameter list on the controller. You can include any variables that are needed with the rest of your JSON data. So your controller would look like this:

BaseOutput ApiReqeust(BaseInput Input_data)

Your BaseInput model would look like this:

public class BaseInput
{ 
 [JsonProperty(PropertyName = "partnerId")]
 public int partnerId { get; set; }
 [JsonProperty(PropertyName = "method")]
 public string method { get; set; }
 [JsonProperty(PropertyName = "hashcode")]
 public string hashCode { get; set; }
 [JsonProperty(PropertyName = "accountid")]
 public int accountId { get; set; }
}

And your JSON data would ultimately look like this:

{
 "partnerId": 1,
 "method": "check",
 "hashcode": " xxxxxx ",
 "accountId": "11111111"
}

I also agree with the comments that you do not need the [JsonArray] attribute as you are not passing any arrays.

answered Apr 20, 2018 at 15:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.