0

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.

1
  • 2
    Ideally your controller will accept one parameter which is an object that has properties that map to the json. Commented Mar 22, 2018 at 19:58

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Would this work if the text being sent is a JSON string? Or is this specifically how that string gets broken down?
@mightynifty It should. There does appear to be a mistake in the JSON you posted above though(double \\ before idList).
Shouldn't you add the attribute FromBody to your method signature for it to work?
@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 well
2

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

2 Comments

He's sending this from a Java application. Would he not have to include "http://localhost:Port/Addpoint"?
Yes he should depending on the URL of the api
0
.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.

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.