Package org.javalite.http
Class Request
java.lang.Object
org.javalite.http.Request
This class provides static convenience methods for simple HTTP requests.
- Author:
- Igor Polevoy
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T extends Request>
TConvenience method to set a ""Content-Type" to "application/json" value.<T extends Request>
TSet a user authentication<T extends Request>
TSets a user and password for basic authentication.byte[]
bytes()
Fetches response content from server as bytes.protected <T extends Request>
Tconnect()
<T extends Request>
TcontentType(String value)
Convenience method to set a ""Content-Type" header.void
dispose()
protected <T extends Request>
TdoConnect(HttpURLConnection connection)
Returns input stream to read server response from.protected abstract String
<T extends Request>
TSets an HTTP header - call before making a request.headers()
Returns HTTP headers as sent by server.jsonList()
Converts a server response to an instance ofJSONList
.Converts a server response to an instance ofJSONList
.jsonMap()
Converts a server response to an instance ofJSONMap
.Converts a server response to an instance ofJSONMap
.<T extends Request>
TAdds a parameter to the request as in a HTML form.params()
<T extends Request>
TConvenience method to add multiple parameters to the request.<T extends Request>
Tredirect(boolean redirect)
Configures this request to follow redirects.int
Returns HTTP response code.Returns response message from server, such as "OK", or "Created", etc.text()
Fetches response content from server as String.Fetches response content from server as String.
-
Field Details
-
redirect
protected boolean redirect -
url
-
-
Constructor Details
-
Request
-
-
Method Details
-
header
Sets an HTTP header - call before making a request.- Parameters:
name
- header namevalue
- header value.- Returns:
- self.
-
applicationJson
Convenience method to set a ""Content-Type" to "application/json" value.- Returns:
- self
-
contentType
Convenience method to set a ""Content-Type" header.- Parameters:
value
- value for a "Content-Type" header.- Returns:
- self
-
redirect
Configures this request to follow redirects. Default isfalse
.- Parameters:
redirect
- true to follow, false to not.- Returns:
- self
- See Also:
- HttpURLConnection.html#instanceFollowRedirects
-
getInputStream
Returns input stream to read server response from.- Returns:
- input stream to read server response from.
-
headers
Returns HTTP headers as sent by server.- Returns:
- HTTP headers as sent by server.
-
responseCode
public int responseCode()Returns HTTP response code.- Returns:
- HTTP response code.
-
responseMessage
Returns response message from server, such as "OK", or "Created", etc.- Returns:
- response message from server, such as "OK", or "Created", etc.
-
bytes
public byte[] bytes()Fetches response content from server as bytes.- Returns:
- response content from server as bytes.
-
text
Fetches response content from server as String. UTF-8 is used as a default encoding.- Returns:
- response content from server as String.
-
text
Fetches response content from server as String.- Parameters:
encoding
- - name of supported charset to apply when reading data.- Returns:
- response content from server as String.
-
dispose
public void dispose()This method is already called fromtext()
andbytes()
, you do not have to call it if you use those methods. However, if you usegetInputStream()
, call this method in those cases when you think you did not read entire content from the stream. This method clears all remaining data in connections after reading a response. This will help keep-alive work smoothly. -
connect
-
basic
Sets a user and password for basic authentication.- Parameters:
user
- user.password
- password.- Returns:
- self.
-
basic
Set a user authentication- Parameters:
user
- user- Returns:
- self
-
doConnect
- Throws:
IOException
-
getMethod
-
params
-
params
Convenience method to add multiple parameters to the request. Names and values alternate: name1, value1, name2, value2, etc.- Parameters:
namesAndValues
- names/values of multiple fields to be added to the request.- Returns:
- self
-
param
Adds a parameter to the request as in a HTML form.- Parameters:
name
- name of parametervalue
- value of parameter- Returns:
- self
-
jsonMap
Converts a server response to an instance ofJSONMap
.- Parameters:
encoding
- pass to specify encoding. UseStandardCharsets
name()
method to get provide specific charsets.- Returns:
- instance of
JSONMap
-
jsonMap
Converts a server response to an instance ofJSONMap
. UTF-8 is used as a default encoding.- Returns:
- instance of
JSONMap
-
jsonList
Converts a server response to an instance ofJSONList
.- Returns:
- instance of
JSONList
-
jsonList
Converts a server response to an instance ofJSONList
.- Parameters:
encoding
- pass to specify encoding. UseStandardCharsets
name()
method to get provide specific charsets.- Returns:
- instance of
JSONList
-