I have an api that needs three parameters, a string(dataType),a class(eloanInput) and a bool(isCluster)
public HttpResponseMessage getEloanExcel(string dataType, EloanInput eloanInput, bool isCluster = false)
{
var exportDataService = new ExportDataService();
var exportExcel = new ExportExcel(dataType);
var inputParams = new CaseSearch.EloanInput();
inputParams.SEARCH_TYPE = eloanInput.SEARCH_TYPE;
inputParams.COUNTY_ID = eloanInput.COUNTY_ID;
inputParams.TOWN_ID = eloanInput.TOWN_ID;
inputParams.HOUSE_TYPES = (eloanInput.HOUSE_TYPES[0] == "-1" && eloanInput.HOUSE_TYPES.Count() == 1) ? null : eloanInput.HOUSE_TYPES;
inputParams.HouseProject = (eloanInput.HouseProject[0] == "-1" && eloanInput.HouseProject.Count() == 1) ? null : eloanInput.HouseProject;
inputParams.PUB_START_DT = eloanInput.PUB_START_DT;
inputParams.PUB_END_DT = eloanInput.PUB_END_DT;
}
just like the image below:
I have a problem when testing the API in Postman, I use key, value method to pass my parameters, and only eloanInput gets null, it doesn't get the values I passed to it, but other parameters indeed get the values by the postman.
[Key] [Value]
dataType 'Eloan'
eloanInput { "SEARCH_TYPE": 2,
"COUNTY_ID": "-1",
"TOWN_ID": "-1",
"PUB_START_DT": "2006/11/22",
"PUB_END_DT": "2006/12/15",
"HOUSE_TYPES": ["01", "02", "03"],
"HouseProject": [1, 2, 3, 4, 5],
"DONE_START_DT": "88",
"DONE_END_DT": "108",
"FLOOR_FROM": "1",
"FLOOR_TO": "18",
"BUILD_AREA_FROM": "2",
"BUILD_AREA_TO": "48",
"CASE_TYPE": [1, 2, 3, 4, 5],
"CASE_CATEGORY": [1, 2, 3, 4],
"CASENM_KEYWORD": "12"}
isCluster false
Postman Request:
To send a query parameter, add it directly to the URL or open Params and enter the name and value. When you enter your query parameters in either the URL or the Params fields, these values will update everywhere they're used in Postman. Parameters aren't automatically URL-encoded.
@FormParam is for application/x-www-form-urlencoded content type. The form-data button you selected in Postman will create mulitpart data. These are two different animals. If you want your example to work, you should select the x-www-form-urlencoded button, then start adding key/value pairs.
To send a JSON object or an array as a parameter in HTTP requests ( GET or POST ) in JavaScript, you first need to convert it into an string using the JSON. stringify() method. Next, use the encodeURIComponent() method to encode the JSON string.
I would send that data from the request body (with the help of [FromBody]) and by changing it to [HttpPost]
method instead of [HttpGet]
.
Your updated method code:
[HttpPost]
public HttpResponseMessage getEloanExcel(string dataType, [FromBody] EloanInput eloanInput, bool isCluster = false)
{
// your code
}
and the request from Postman:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With