question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get the body payload?

See original GitHub issue

I am hoping this is a defect against documentation.

I am unable to retrieve the body payload for a POST operation.

Using the Swagger-Petstore sample, the POST to /pet has a body parameter defined as:

{
  "in": "body",
  "name": "body",
  "description": "Pet object that needs to be added to the store",
  "required": true,
  "schema": {
     "$ref": "#\/definitions\/Pet"
  }
}

Here is my sample code:

import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import io.swagger.parser.SwaggerParser;

public class PetPostBody {

    public static void main(String[] args) {
	Swagger swagger = new SwaggerParser().read("http://petstore.swagger.io/v2/swagger.json");

	Path pet = swagger.getPath("/pet");
	Operation post = pet.getPost();
	Parameter body = null;
	for (Parameter param : post.getParameters()) {
	    if (param.getIn().equals("body"))
		body = param;
	}
	body.get???
    }
}

How do I retrieve the payload?

The only things that I can see (in debugger) for body is schema but that is access protected(?), and under that is genericRef (access private) that points to the definitions.

What am I missing?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
benken-parasoftcommented, Oct 14, 2017

Forgive me if I misunderstand your question but in case this helps . . . In swagger-parser 1, I use io.swagger.models.parameters.BodyParameter.getSchema()

List<Parameter> parameters = operation.getParameters();
if (parameters != null) {
    for (Parameter parameter: parameters) {
        if (parameter instanceof BodyParameter) {
            BodyParameter bp = (BodyParameter) parameter;
            Model schema = bp.getSchema();

In swagger-parser 2, I use io.swagger.oas.models.media.MediaType.getSchema()

RequestBody bodyParam = operation.getRequestBody();
if (bodyParam != null) {
    Content content = bodyParam.getContent();
    if (content != null) {
        for (Map.Entry<String, MediaType> request : content.entrySet()) {
            MediaType mediaType = request.getValue();
            Schema<?> schema = mediaType.getSchema()
0reactions
SiKingcommented, Oct 16, 2017

Looks like schema.get$ref() and schema.getReference() get you the same thing … at least in my case. So the approach I posted above is the correct way forward?

Our development has decided to use an API framework that only generates OAI2, and only partially at that. I do not have any say in that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP GET with request body - Stack Overflow
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing...
Read more >
GET - HTTP - MDN Web Docs
It is better to just avoid sending payloads in GET requests. Request has body, No. Successful response has body, Yes. Safe, Yes. Idempotent,...
Read more >
Request bodies in GET requests - Evert Pot
A payload within a GET request message has no defined semantics. That sentence could easily suggest that there's no specific behavior ...
Read more >
REST API - HTTP GET with Request Body - Roy Tutorials
Generally payload in the body is sent for POST, PUT, etc. http methods where you need to create a new resource or update...
Read more >
Add a Request Body to a POST Request | API Connector
As part of a POST , PUT , or PATCH request, a data payload can be sent to the server in the body...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found