I have an issue that I've been trying to solve. I'm trying to send data from a java application to a web server, but I can't figure out how to actually send it. The java code is as follows:
String hStr = "{\"id\":2,\"name\":\"John\",\"height\":36.72342538,\"width\":2.99999998,\"frequency\":871.07,\\"idList\":[],\"level\":0.0}";
House ap = toJsonMap.readValue(hStr, House.class);
when: "ask the server to add a house from the request"
def response = server.httpClient.requestSpec { spec ->
spec.body { b ->
b.text(hStr)
b.type("application/json")
}
}
.post("//modeling/housing/{hid}/prop/point/in");
I then have the C# read this code like this:
[Route("modeling/housing/{hid}/prop/point/in")]
[HttpPost]
public HttpResponseMessage AddPoint(int hid, int id, string name, double height, double width, double frequency, List<int> idList, double level)
{
DAL.House h = new DAL.House();
try
{
using (DAL.Entities context = DAL.Entities.CreateContextForComplex(said))
{
if (!context.Houses.Where(a => a.Id == id).Any())
{
h.Name = name;
h.Height = height;
h.Width = width;
h.Frequency = frequency;
h.IdList= idList;
h.Level = level;
h.LastModified = System.DateTime.UtcNow;
context.Houses.Add(ap);
context.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, ap);
}
else
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, "Housing id already exists");
}
}
}
catch (EntityException)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, "Entity Exception");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
}
}
I just can't figure out how to get the data from this post. Particularly getting all of the different types of variables. I found a lot of different answers, but nothing seems to work.
-
2Ideally your controller will accept one parameter which is an object that has properties that map to the json.Crowcoder– Crowcoder2018年03月22日 19:58:12 +00:00Commented Mar 22, 2018 at 19:58
3 Answers 3
Most likely you need to create a class that has properties matching the incoming request post body's object properties. For example:
public class House
{
public int Hid { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public double Height { get; set; }
public double Width { get; set; }
public double Frequency { get; set; }
public List<int> IdList { get; set; }
public double Level { get; set; }
}
Then you would update your method signature as follows:
public HttpResponseMessage AddPoint(House house)
-
Would this work if the text being sent is a JSON string? Or is this specifically how that string gets broken down?mightynifty– mightynifty2018年03月22日 20:06:08 +00:00Commented Mar 22, 2018 at 20:06
-
@mightynifty It should. There does appear to be a mistake in the JSON you posted above though(double \\ before idList).peinearydevelopment– peinearydevelopment2018年03月22日 20:08:29 +00:00Commented Mar 22, 2018 at 20:08
-
Shouldn't you add the attribute FromBody to your method signature for it to work?Kevin Avignon– Kevin Avignon2018年03月22日 21:38:42 +00:00Commented Mar 22, 2018 at 21:38
-
@KevinAvignon Only if you are using Asp.Net Core. See documentation. Also note that with the next version of core, this will go away as wellpeinearydevelopment– peinearydevelopment2018年03月22日 22:00:41 +00:00Commented Mar 22, 2018 at 22:00
Try to create a class that represents all the properties in the JSON Object:
public class YouClass
{
public int Id { get; set; }
public string Name { get; set; }
public string Height { get; set; }
......
// add others
}
Then in your controller:
public class HousingController : ApiController
{
[Route("AddPoint")
[HttpPost]
public HttpResponseMessage AddPoint([FromBody] YourClass)
{
}
}
Then modify the URL of API your are calling:
.post("api/Housing/Addpoint")
Your URL might be different, you might use : http://localhost:Port/api/Housing/Addpoint
and the port. Make sure you try it in browser first or use Postman. Check this
-
He's sending this from a Java application. Would he not have to include
"http://localhost:Port/Addpoint"
?Brandon Miller– Brandon Miller2018年03月22日 20:08:31 +00:00Commented Mar 22, 2018 at 20:08 -
Yes he should depending on the URL of the apiHussein Salman– Hussein Salman2018年03月22日 20:09:15 +00:00Commented Mar 22, 2018 at 20:09
.post("//modeling/housing/{hid}/prop/point/in");
This line of code should give you a timeout in your java, if this is exactly how you have it typed. What you really want here is something more like:
.post("http://localhost:PortNumber/modeling/housing/"+ ap.id +"/prop/point/in");
Where PortNumber is the port your web api is running on, and ap.Id is the Id of the record you are trying to modify.
After you have corrected your endpoint situation, then move on to the other answers and use JSON.Net to deserialize your JSON back into a class.