Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to passing class object Postman

Tags:

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:

webapi

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:

postman api testing

like image 903
lwf Avatar asked Jun 30 '19 08:06

lwf


People also ask

How can we send object in query params Postman?

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.

How can I pass FormParam in Postman?

@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.

How do you send an object in a GET request?

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.


1 Answers

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:

enter image description here

like image 133
Prashant Pimpale Avatar answered Nov 03 '22 01:11

Prashant Pimpale