File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed
org/javawebstack/httpclient Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 99import java .net .HttpURLConnection ;
1010import java .net .URL ;
1111import java .nio .charset .StandardCharsets ;
12+ import java .util .Base64 ;
1213import java .util .HashMap ;
1314import java .util .Map ;
1415
@@ -65,6 +66,21 @@ public HTTPRequest authorization(String type, String value){
6566 return header ("Authorization" , type + " " + value );
6667 }
6768
69+ public HTTPRequest basicAuthorization (String username , String password ){
70+ return authorization ("Basic" , Base64 .getEncoder ().encodeToString ((username +":" +password ).getBytes ()));
71+ }
72+
73+ /*
74+ * TODO: Currently not supporting nested form-data
75+ * */
76+ public HTTPRequest formBody (Map <String , String > data ){
77+ return body (new QueryString (data ).toString ());
78+ }
79+
80+ public HTTPRequest formBody (QueryString query ) {
81+ return body (query .toString ());
82+ }
83+
6884 public HTTPRequest bearer (String token ){
6985 return authorization ("Bearer" , token );
7086 }
@@ -78,11 +94,14 @@ public int status(){
7894 }
7995
8096 public byte [] bytes (){
97+ if (requestBody == null )
98+ execute ();
99+
81100 return responseBody ;
82101 }
83102
84103 public String string (){
85- return new String (responseBody , StandardCharsets .UTF_8 );
104+ return new String (bytes () , StandardCharsets .UTF_8 );
86105 }
87106
88107 public <T > T json (Class <T > type ){
Original file line number Diff line number Diff line change 1+ import org .javawebstack .httpclient .HTTPClient ;
2+
3+ public class test {
4+ public static void main (String [] args ) {
5+ HTTPClient client = new HTTPClient ();
6+ System .out .println ("Test: " +
7+ client .get ("https://postman-echo.com/" )
8+ .string ());
9+ }
10+ }
You can’t perform that action at this time.
0 commit comments