1

Using swagger editor I created a post call to consume a json object that I want to simply load into a db but when I run the call I get an empty json object.

This is the parameters portion of my json swagger for the post

                "parameters": [
                {
                    "in": "body",
                    "name": "body",
                    "description": "Add question to collection",
                    "required": true,
                    "schema": { "type" : "object", "additionalProperties" : {}
                    }
                }
            ],

It then creates a "Body" model, but I am not able to see the json that was part of the post:

 @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2016-01-22T20:49:03.229Z")
public class Body   {




  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Body body = (Body) o;
    return true;
  }

  @Override
  public int hashCode() {
    return Objects.hash();
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Body {\n");

    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}

If I remove the text/json from consume and generated my code again and I still see an issue with the body model and being able to pull in json. If you look at the toString method it shows hard coded values, so I dont see how I can pull the json from the post with the post method only taking in the Body and securitycontext.

eternal_student
  • 626
  • 4
  • 18
user1953901
  • 21
  • 1
  • 5

2 Answers2

0

Before you send your data you should check HTTP ACCEPT method which you set in swagger when you send your data.

There should be Several Accept method which their behaviour distinct from each other when sending data to server.

Thus for application/JSON : Data are part of body.

form-data and x-www-form-urlencoded : Data are part of header.

I haven't adaquate Java experience to give correct code to obtain json into related jSON Object but How to convert HTTP Request Body into JSON Object in Java this answer could help.

Please Check following RFCs for further information

form-data related RFC https://www.rfc-editor.org/rfc/rfc7578

x-www-form-urlencoded related RFC https://datatracker.ietf.org/doc/html/draft-hoehrmann-urlencoded-01

application/JSON related rfc https://www.ietf.org/rfc/rfc4627.txt

UPDATED

Related curl command : I get the command from swagger live demo http://petstore.swagger.io/#/pet paste your json to it and change url, secret key give it a try!

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
  "id": 0,
  "category": {
    "id": 0,
    "name": "string"
  },
  "name": "doggie",
  "photoUrls": [
    "string"
  ],
  "tags": [
    {
      "id": 0,
      "name": "string"
    }
  ],
  "status": "available"
}' 'http://petstore.swagger.io/v2/pet?api_key=special-key'
Community
  • 1
  • 1
FZE
  • 1,587
  • 12
  • 35
  • When I run the curl command it runs but when I display body.tostring() I am not seeing the json I am seeing what is hardcoded: class Body {}, so my question is what do I need to do to display the json. – user1953901 Jan 25 '16 at 22:22
0

I am a little confused about the http accept, when using this swagger snippet:

            "post": {
            "tags": [
                "AskGrey"
            ],
            "summary": "Create new askgrey.com question",
            "operationId": "postAskGrey",
            "consumes": [
                "application/json",
                "text/json"
            ],
            "produces": [
                "application/json"
            ],
            "parameters": [
                {
                    "in": "body",
                    "name": "body",
                    "description": "Add question to collection",
                    "required": true,
                    "schema": { "type" : "object", "additionalProperties" : {}
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "type": "object"

                    }
                }
            },
            "deprecated": false
        }

The method generated is the following:

      @Override
  @POST
  @Consumes("application/json")
  public Response postAskGrey(Body body,SecurityContext securityContext)
  throws NotFoundException {

So based on all this I am not sure how to pull in the post body info, normally I would grab what I need from the http request but with swagger I cant seem to figure out how that gets used.

user1953901
  • 21
  • 1
  • 5
  • First of all you can edit your answer for sharing new code, it would be more organized. Second, You may remove `text/json` for consume, and Body body should give you desired json, but you should send the json in `body = '{"key1" : data1, "key2": data2}'` format. Naturally swagger puts body variable in ui don't worry about it, but `text/json` may break the things and you may sending your json with that. Be sure you are using application/json while sending from swagger. So following answer is more clear you may give it a try if something doesnt work – FZE Jan 25 '16 at 03:38
  • Btw, is your swagger configured to show related curl command ? If it is please copy and paste to your command line to be sure is that work or not ? There may be server misconfiguration in swagger or swagger server configuration which corrupts the request. – FZE Jan 25 '16 at 03:41