Alex
Alex

Reputation: 199

POST request doesn't accept a specific formatation of JSON, even though it's the same

I'm trying to make a POST request to my API with the following Request Bodies:

{
    "data": [
        {
            "source": "A",
            "target": "B",
            "distance": 10
        },
        {
            "source": "A",
            "target": "C",
            "distance": 15
        }
    ]
}

and

{​
  "data": [
    {​
      "source": "A", "target": "B", "distance": 6
    }​,
    {​
      "source": "A", "target": "E", "distance": 4
    }
  ]
}

The first one works, but the second one doesn't, and returns the following error:

"message": "JSON parse error: Unexpected character ('​' (code 8203 / 0x200b)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('​' (code 8203 / 0x200b)): was expecting double-quote to start field name\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 5]"

That's my Controller's POST function:

@PostMapping()
    public ResponseEntity<Graph> saveGraph(@RequestBody Graph graph){
        System.out.println(graph);
        Graph obj = graphService.saveGraph(graph);
        return ResponseEntity.status(HttpStatus.CREATED).body(obj);
    }

Why is that?

Upvotes: 2

Views: 463

Answers (1)

jedeen
jedeen

Reputation: 198

0x200b is the Unicode value for a zero-width space, which it seems like the Jackson JSON parser is not able to handle. Zero-width spaces aren't usually visible in most editors by default, which results in confusing situations like the one you've encountered.

Your text or code editor might have a setting to show hidden characters; I pasted both of your snippets into Sublime Text, and it showed me the hidden characters that are causing the problem:

screenshot of the code in the previous post, showing multiple zero-width characters in the second JSON sample

Removing the four zero-width characters present in the second JSON sample should resolve the error, either manually or by running the JSON sample through a regex to strip out the 0x200b values before parsing it with Jackson.

Upvotes: 3

Related Questions