@@ -33,6 +33,8 @@ a copy of this software and associated documentation files (the
3333import com .mashape .unirest .request .body .MultipartBody ;
3434import com .mashape .unirest .request .body .RawBody ;
3535import com .mashape .unirest .request .body .RequestBodyEntity ;
36+ import org .json .JSONArray ;
37+ import org .json .JSONObject ;
3638
3739import java .io .File ;
3840import java .util .Collection ;
@@ -41,106 +43,127 @@ a copy of this software and associated documentation files (the
4143
4244public class HttpRequestWithBody extends HttpRequest {
4345
44- public HttpRequestWithBody (HttpMethod method , String url ) {
45- super (method , url );
46- }
47-
48- @ Override
49- public HttpRequestWithBody routeParam (String name , String value ) {
50- super .routeParam (name , value );
51- return this ;
52- }
53-
54- @ Override
55- public HttpRequestWithBody header (String name , String value ) {
56- return (HttpRequestWithBody ) super .header (name , value );
57- }
58-
59- @ Override
60- public HttpRequestWithBody headers (Map <String , String > headers ) {
61- return (HttpRequestWithBody ) super .headers (headers );
62- }
63-
64- @ Override
65- public HttpRequestWithBody basicAuth (String username , String password ) {
66- super .basicAuth (username , password );
67- return this ;
68- }
69-
70- @ Override
71- public HttpRequestWithBody queryString (Map <String , Object > parameters ) {
72- return (HttpRequestWithBody ) super .queryString (parameters );
73- }
74-
75- @ Override
76- public HttpRequestWithBody queryString (String name , Object value ) {
77- return (HttpRequestWithBody ) super .queryString (name , value );
78- }
79-
80- public MultipartBody field (String name , Collection <?> value ) {
81- MultipartBody body = new MultipartBody (this ).field (name , value );
82- this .body = body ;
83- return body ;
84- }
85-
86- public MultipartBody field (String name , Object value ) {
87- return field (name , value , null );
88- }
89-
90- public MultipartBody field (String name , File file ) {
91- return field (name , file , null );
92- }
93-
94- public MultipartBody field (String name , Object value , String contentType ) {
95- MultipartBody body = new MultipartBody (this ).field (name , (value == null ) ? "" : value .toString (), contentType );
96- this .body = body ;
97- return body ;
98- }
99-
100- public MultipartBody field (String name , File file , String contentType ) {
101- MultipartBody body = new MultipartBody (this ).field (name , file , contentType );
102- this .body = body ;
103- return body ;
104- }
105-
106- public MultipartBody fields (Map <String , Object > parameters ) {
107- MultipartBody body = new MultipartBody (this );
108- if (parameters != null ) {
109- for (Entry <String , Object > param : parameters .entrySet ()) {
110- if (param .getValue () instanceof File ) {
111- body .field (param .getKey (), (File ) param .getValue ());
112- } else {
113- body .field (param .getKey (), (param .getValue () == null ) ? "" : param .getValue ().toString ());
114- }
115- }
116- }
117- this .body = body ;
118- return body ;
119- }
120-
121- public RequestBodyEntity body (JsonNode body ) {
122- return body (body .toString ());
123- }
124-
125- public RequestBodyEntity body (String body ) {
126- RequestBodyEntity b = new RequestBodyEntity (this ).body (body );
127- this .body = b ;
128- return b ;
129- }
130-
131- public RequestBodyEntity body (Object body ) {
132- ObjectMapper objectMapper = (ObjectMapper ) Options .getOption (Option .OBJECT_MAPPER );
133-
134- if (objectMapper == null ) {
135- throw new RuntimeException ("Serialization Impossible. Can't find an ObjectMapper implementation." );
136- }
137-
138- return body (objectMapper .writeValue (body ));
139- }
140-
141- public RawBody body (byte [] body ) {
142- RawBody b = new RawBody (this ).body (body );
143- this .body = b ;
144- return b ;
145- }
46+ public HttpRequestWithBody (HttpMethod method , String url ) {
47+ super (method , url );
48+ }
49+
50+ @ Override
51+ public HttpRequestWithBody routeParam (String name , String value ) {
52+ super .routeParam (name , value );
53+ return this ;
54+ }
55+
56+ @ Override
57+ public HttpRequestWithBody header (String name , String value ) {
58+ return (HttpRequestWithBody ) super .header (name , value );
59+ }
60+
61+ @ Override
62+ public HttpRequestWithBody headers (Map <String , String > headers ) {
63+ return (HttpRequestWithBody ) super .headers (headers );
64+ }
65+
66+ @ Override
67+ public HttpRequestWithBody basicAuth (String username , String password ) {
68+ super .basicAuth (username , password );
69+ return this ;
70+ }
71+
72+ @ Override
73+ public HttpRequestWithBody queryString (Map <String , Object > parameters ) {
74+ return (HttpRequestWithBody ) super .queryString (parameters );
75+ }
76+
77+ @ Override
78+ public HttpRequestWithBody queryString (String name , Object value ) {
79+ return (HttpRequestWithBody ) super .queryString (name , value );
80+ }
81+
82+ public MultipartBody field (String name , Collection <?> value ) {
83+ MultipartBody body = new MultipartBody (this ).field (name , value );
84+ this .body = body ;
85+ return body ;
86+ }
87+
88+ public MultipartBody field (String name , Object value ) {
89+ return field (name , value , null );
90+ }
91+
92+ public MultipartBody field (String name , File file ) {
93+ return field (name , file , null );
94+ }
95+
96+ public MultipartBody field (String name , Object value , String contentType ) {
97+ MultipartBody body = new MultipartBody (this ).field (name , (value == null ) ? "" : value .toString (), contentType );
98+ this .body = body ;
99+ return body ;
100+ }
101+
102+ public MultipartBody field (String name , File file , String contentType ) {
103+ MultipartBody body = new MultipartBody (this ).field (name , file , contentType );
104+ this .body = body ;
105+ return body ;
106+ }
107+
108+ public MultipartBody fields (Map <String , Object > parameters ) {
109+ MultipartBody body = new MultipartBody (this );
110+ if (parameters != null ) {
111+ for (Entry <String , Object > param : parameters .entrySet ()) {
112+ if (param .getValue () instanceof File ) {
113+ body .field (param .getKey (), (File ) param .getValue ());
114+ } else {
115+ body .field (param .getKey (), (param .getValue () == null ) ? "" : param .getValue ().toString ());
116+ }
117+ }
118+ }
119+ this .body = body ;
120+ return body ;
121+ }
122+
123+ public RequestBodyEntity body (JsonNode body ) {
124+ return body (body .toString ());
125+ }
126+
127+ public RequestBodyEntity body (String body ) {
128+ RequestBodyEntity b = new RequestBodyEntity (this ).body (body );
129+ this .body = b ;
130+ return b ;
131+ }
132+
133+ public RequestBodyEntity body (Object body ) {
134+ ObjectMapper objectMapper = (ObjectMapper ) Options .getOption (Option .OBJECT_MAPPER );
135+
136+ if (objectMapper == null ) {
137+ throw new RuntimeException ("Serialization Impossible. Can't find an ObjectMapper implementation." );
138+ }
139+
140+ return body (objectMapper .writeValue (body ));
141+ }
142+
143+ public RawBody body (byte [] body ) {
144+ RawBody b = new RawBody (this ).body (body );
145+ this .body = b ;
146+ return b ;
147+ }
148+
149+ /**
150+ * Sugar method for body operation
151+ *
152+ * @param body raw org.JSONObject
153+ * @return RequestBodyEntity instance
154+ */
155+ public RequestBodyEntity body (JSONObject body ) {
156+ return body (body .toString ());
157+ }
158+
159+ /**
160+ * Sugar method for body operation
161+ *
162+ * @param body raw org.JSONArray
163+ * @return RequestBodyEntity instance
164+ */
165+ public RequestBodyEntity body (JSONArray body ) {
166+ return body (body .toString ());
167+ }
168+
146169}
0 commit comments