I am using sitebricks-client to interact with REST APIs in Java. I need to do a POST with a non-empty body. How do I do that in sitebricks?
Asked
Active
Viewed 272 times
2 Answers
1
You have not specified what kind of request body you are trying to post. If you are trying to send a String with a Content-Type of "text/plain", then the following should work:
String body = "Request body.";
WebResponse response = web.clientOf(url)
.transports(String.class)
.over(Text.class)
.post(body);
If you are trying to send data of a particular type that you have already serialized to a String, you can set the Content-Type header manually:
String body = "{}";
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
WebResponse response = web.clientOf(url, headers)
.transports(String.class)
.over(Text.class)
.post(body);
If you have a Map containing data that you would like to send to the server with a Content-Type of "application/json", then something like this might be up your alley:
Map body = new HashMap();
// Fill in body with data
WebResponse response = web.clientOf(url)
.transports(Map.class)
.over(Json.class)
.post(body);
There are two important points to pay attention to in the examples above:
- The value passed to the
post
method should be of the type passed to thetransports
method. - The class that you pass to the
over
method determines the default value of the Content-Type header and the way in which the value passed to thepost
method is serialized. The class should be a subclass ofcom.google.sitebricks.client.Transport
, and you will probably want to choose one of the classes found in thecom.google.sitebricks.client.transport
package.

Brad Lord
- 841
- 6
- 11
0
I have note tried it but there is a post() method in webclient.
web.clientOf("http://google.com")
...
.post(...);
Check out the source on github
Rgds

Thomas
- 1,410
- 10
- 24
-
There is a `POST` method but I don't see how it allows me to set the body of the request which my original question asks - how to do a `POST` with non-empty body – pathikrit Sep 23 '12 at 23:56