简体   繁体   中英

Writing HTTP POST Request in Java

JsonObject.addProperty(property,value) Clarifications

My current createAuth() HTTP POST Requests works fine as tested with Postman with the following details with the raw JSON content as the Body.

{
    "datetimeAccepted": null,
    "adminAuthorizer": {
        "adminId": 1
    },
    "lock": {
        "id": 1
    }
}

However, I am facing issues with writing the exact same Body as a JsonObject in Java with a snapshot of my code here .

JsonObject authDetails = new JsonObject();
authDetails.addProperty("datetimeAccepted", (Boolean) null);
authDetails.addProperty("adminAuthorizer", // To Write);
authDetails.addProperty("lock", // To Write);

How do I go about writing the Java code for this Body? I thank you in advance for the reply.

You can implement it something like below:

JsonObject authDetails = new JsonObject();
authDetails.addProperty("datetimeAccepted", (Boolean) null);

JsonObject adminDetails = new JsonObject();
adminDetails.addProperty("adminId", 1);
authDetails.add("adminAuthorizer", adminDetails);

JsonObject lockDetails = new JsonObject();
lockDetails.addProperty("id", 1);
authDetails.add("lock", lockDetails);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM