Home > Back-end >  POST request doesn't accept a specific formatation of JSON, even though it's the same
POST request doesn't accept a specific formatation of JSON, even though it's the same

Time:04-30

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?

CodePudding user response:

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.

  • Related