Reputation: 21
so it may be a stupid question for some if not for all, but i have this response header :
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Atr: 10
X-Atr2: 1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 29 Mar 2016 09:17:26 GMT
and the result that goes with it :
{"site": [
{
"id": "1",
"name": "James"
},
{
"id": "2",
"name": "Katia"
},
{
"id": "3",
"name": "Sam"
}
What i want to do is to change HTTP/1.1 200 OK in case i get this i get something like HTTP/1.1 206 you have some results and in the same time have the display the JSON result. and in case that we don't have any results it would be like: HTTP/1.1 400 didn't find any result AND the JSON result would be like :
[]
So what i have tried is the following :
if (response.getSite().size() <= 0) {
responseHttp.sendError(HttpServletResponse.SC_ACCEPTED, "you have some results");
} else {
responseHttp.sendError(HttpServletResponse.SC_ACCEPTED, "you have some results");}
The thing is in both cases it edited HTTP/1.1 but doesn't display any message nor the JSON results.
hope i well explained my problème thank you for your time and help.
Upvotes: 0
Views: 206
Reputation: 6136
I assume you meant you want to set the response status so I suggest to use something like:
if (response.getSite().size() <= 0) {
responseHttp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Request");
} else {
responseHttp.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
//your other codes for sending JSON response goes here:
}
Upvotes: 0
Reputation: 1091
You just need to set the status code and the message accordingly. You did set SC_ACCEPTED for both cases.
Btw. SC_ACCEPTED
is 202.
For "didn't find any result" you can take SC_NOT_FOUND
(404);
Alternatively you can just use ints
You can find the SC_...
values here.
If you want to send the message AND the json, you might need to embed the json string into the message. Otherwise you might need to just send the json in the body and move the message to the HTTP Status Description in the header. The method sendError(int sc, String msg)
creates a HTML like page with your message embedded (see here).
Upvotes: 1