Message "can't parse JSON. Raw result:" added to response body
Asked Answered
G

3

8

I'm trying to define a file download endpoint using the OpenAPI spec (v3). I am facing two problems. 1) the file is not being downloaded 2) within swagger-ui, the response body shows a message that isn't produced by the server (i.e. can't parse JSON. Raw result:"

Anyone have a clue as to what I am doing wrong?

YAML code snapshot: enter image description here

Output result from swagger-ui running on an AWS server. enter image description here

Galiot answered 9/1, 2018 at 1:3 Comment(2)
Looks like a UI problem. Open an issue in the Swagger UI repository: github.com/swagger-api/swagger-ui/issues.Kwiatkowski
GitHub issue #4098 created.Galiot
G
7

The cause of the anomalous message, I was told, is because application/json is the content media said to be returned in the server's response header. See the header here: enter image description here

The response is actually a text file -- not JSON. The sending server is incorrectly configured. Swagger-UI runtime response handler determines whether the response is valid. So Swagger-UI outputs the "can't parse JSON..." message.

Because of the error, the output to local storage function on the browser is not performed.

For more details, refer to the Swagger-UI issue #4098.

Galiot answered 16/1, 2018 at 0:3 Comment(1)
A simple string is a valid json, you can verify it here jsonlint.com. So swagger should be able to parse such a response, that is a String with content-type application/json. Am I wrong?Whorehouse
A
0

Try this.

   services.AddMvc(config => {
        }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
          .AddJsonOptions(options => {
              options.SerializerSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ssZ";
              options.SerializerSettings.Formatting = Formatting.Indented;
              options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
              options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
          });
Avigation answered 27/9, 2019 at 14:50 Comment(0)
P
0

All I had to do is set the response type

ResponseEntity vs ResponseEntity< String >

Performing answered 26/7, 2024 at 4:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.