Bankelaal Bankelaal - 2 months ago 25
reST (reStructuredText) Question

How to pass JSON as a query parameter in a HTTP request?

I am using RESTEasy stack to implement a REST based client and server. I have a service which handles

POST
request, like below:

@POST
@Path("/this")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String testPost(@QueryParam("thing") String thing) {
...
}


When I call from Postman or REST client the following URI:

http://ip:port/base/this?thing={"id":"abc"}


I always get the error as

java.net.URISyntaxException: Illegal character in query at index (pointing to =)


What is the reason for this?

Answer

If you're trying to parse a POST request, you should use @FormParam instead of @QueryParam. Then, you can make the post request doing something like this (using curl)

curl -i -X POST -H "Content-Type: application/json" -d thing="{\"id\":\"abc\"}" http://ip:port/base/this