Closed jvillemare closed 8 years ago
It should work in 3 different ways:
Webb.setGlobalHeader
sets an HTTP-header for the whole JVM (regarding DavidWebb)webb.setDefaultHeader
for the instance used to create HTTP-requestswebb.get/post/....header()
for a single requestExample code (setting header for a single webb instance):
webb = Webb.create();
webb.setBaseUri(mSyncPrefs.getEndpoint());
webb.setDefaultHeader(Webb.HDR_USER_AGENT, Const.UA);
// take userId and secret to obtain access-token
accessToken = obtainAccessToken(mSyncPrefs);
webb.setDefaultHeader(HDR_ACCESS_TOKEN, accessToken.token);
Example setting header for a single request:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.set(2013, Calendar.NOVEMBER, 24, 23, 59, 33);
Response<Void> response = webb
.get("/headers/in")
.header("x-test-string", SIMPLE_ASCII)
.header("x-test-int", 4711)
.header("x-test-calendar", cal)
.header("x-test-date", cal.getTime())
.param(Webb.HDR_USER_AGENT, Webb.DEFAULT_USER_AGENT)
.asVoid();
assertEquals(200, response.getStatusCode());
Like almost all calls to methods of Request
, it's returning the instance (this) so you can chain the calls. I suppose there is something wrong with your IDE or your code has errors.
Thanks for pointing out the grammatical error!
BTW headers are only overwritten when using the identical name. They are stored in a hash-map and of course multiple headers can be set.
Thank you very much for the quick response,
This is just a note for anyone who encounters the same problem:
Using the Response<Void>
as the variable's type did allow me to add headers( "", "" )
. However, it created a conflict with my try-catch statement below because a Response<JSONObject>
variable is not the same as a JSONObject
. But adding ensureSuccess()
, getBody()
, and changing the variable to JSONObject
solved my problem as shown below:
JSONObject result = webb
.get( "https://www.reddit.com/r/all/new/.json" )
.param( "limit", 1 )
.header( "User-Agent", "Java/8 by <<APP_ID>>" )
.header( "Authorization", "bearer " + token )
.ensureSuccess() // << added
.asJsonObject()
.getBody(); // << added
System.out.println( "OAUTH: Got response: "
+ result.toString().substring( 0, 32 ) + "..." );
String data;
try {
data = result
.getJSONObject( "data" )
.getJSONArray( "children" )
.getJSONObject( 0 )
.getJSONObject( "data" )
.getString( "id" );
} catch ( WebbException ex ) {
...
}
Upon reflection, I do now see how dense I was. Thank you, David, again for helping me with this.
I'm having some trouble setting multiple headers using webb's HTTP library and I don't know if it's because I'm dense or I can't figure out how to cast types or something.
Here's my code:
I'm fairly certain that the second setDefaultHeader() is just overwriting the first statement because I found:
In the API docs, under Request. I tried just inserting .header( "header", "value") after get() and before ensureSuccess(), but then my IDE, NetBeans, said this symbol was not found.
Since header( String, String) is in the Request class, I just thought I could switch out the "Webb webb" declaration for "Request webb" as is shown here, but inserting either
Request or Request<String>
for the webb's variable type produced an error:Also, a little sidenote: In the header()'s Javadocs, there's a grammatical error. It should be "Set (or overwrite) an HTTP header value."