@@ -55,10 +55,9 @@ public final class RequestTemplate implements Serializable {
5555 private String target ;
5656 private boolean resolved = false ;
5757 private UriTemplate uriTemplate ;
58- private BodyTemplate bodyTemplate ;
5958 private HttpMethod method ;
6059 private transient Charset charset = Util .UTF_8 ;
61- private byte [] body ;
60+ private Request . Body body = Request . Body . empty () ;
6261 private boolean decodeSlash = true ;
6362 private CollectionFormat collectionFormat = CollectionFormat .EXPLODED ;
6463
@@ -83,15 +82,13 @@ public RequestTemplate() {
8382 */
8483 private RequestTemplate (String target ,
8584 UriTemplate uriTemplate ,
86- BodyTemplate bodyTemplate ,
8785 HttpMethod method ,
8886 Charset charset ,
89- byte [] body ,
87+ Request . Body body ,
9088 boolean decodeSlash ,
9189 CollectionFormat collectionFormat ) {
9290 this .target = target ;
9391 this .uriTemplate = uriTemplate ;
94- this .bodyTemplate = bodyTemplate ;
9592 this .method = method ;
9693 this .charset = charset ;
9794 this .body = body ;
@@ -109,7 +106,7 @@ private RequestTemplate(String target,
109106 public static RequestTemplate from (RequestTemplate requestTemplate ) {
110107 RequestTemplate template =
111108 new RequestTemplate (requestTemplate .target , requestTemplate .uriTemplate ,
112- requestTemplate .bodyTemplate , requestTemplate . method , requestTemplate .charset ,
109+ requestTemplate .method , requestTemplate .charset ,
113110 requestTemplate .body , requestTemplate .decodeSlash , requestTemplate .collectionFormat );
114111
115112 if (!requestTemplate .queries ().isEmpty ()) {
@@ -137,7 +134,6 @@ public RequestTemplate(RequestTemplate toCopy) {
137134 this .headers .putAll (toCopy .headers );
138135 this .charset = toCopy .charset ;
139136 this .body = toCopy .body ;
140- this .bodyTemplate = toCopy .bodyTemplate ;
141137 this .decodeSlash = toCopy .decodeSlash ;
142138 this .collectionFormat =
143139 (toCopy .collectionFormat != null ) ? toCopy .collectionFormat : CollectionFormat .EXPLODED ;
@@ -225,9 +221,7 @@ public RequestTemplate resolve(Map<String, ?> variables) {
225221 }
226222 }
227223
228- if (this .bodyTemplate != null ) {
229- resolved .body (this .bodyTemplate .expand (variables ));
230- }
224+ resolved .body (this .body .expand (variables ));
231225
232226 /* mark the new template resolved */
233227 resolved .resolved = true ;
@@ -261,7 +255,7 @@ public Request request() {
261255 if (!this .resolved ) {
262256 throw new IllegalStateException ("template has not been resolved." );
263257 }
264- return Request .create (this .method , this .url (), this .headers (), this .body (), this . charset );
258+ return Request .create (this .method , this .url (), this .headers (), this .requestBody () );
265259 }
266260
267261 /**
@@ -541,9 +535,7 @@ public List<String> variables() {
541535 }
542536
543537 /* body */
544- if (this .bodyTemplate != null ) {
545- variables .addAll (this .bodyTemplate .getVariables ());
546- }
538+ variables .addAll (this .body .getVariables ());
547539
548540 return variables ;
549541 }
@@ -717,20 +709,11 @@ public Map<String, Collection<String>> headers() {
717709 * @param bodyData to send, can be null.
718710 * @param charset of the encoded data.
719711 * @return a RequestTemplate for chaining.
712+ * @deprecated use {@link RequestTemplate#body(feign.Request.Body)} instead
720713 */
714+ @ Deprecated
721715 public RequestTemplate body (byte [] bodyData , Charset charset ) {
722-
723- /*
724- * since the body is being set directly, we need to clear out any existing body template
725- * information to prevent unintended side effects.
726- */
727- this .bodyTemplate = null ;
728- this .charset = charset ;
729- this .body = bodyData ;
730-
731- /* calculate the content length based on the data provided */
732- int bodyLength = bodyData != null ? bodyData .length : 0 ;
733- header (CONTENT_LENGTH , String .valueOf (bodyLength ));
716+ this .body (Request .Body .encoded (bodyData , charset ));
734717
735718 return this ;
736719 }
@@ -740,28 +723,49 @@ public RequestTemplate body(byte[] bodyData, Charset charset) {
740723 *
741724 * @param bodyText to send.
742725 * @return a RequestTemplate for chaining.
726+ * @deprecated use {@link RequestTemplate#body(feign.Request.Body)} instead
743727 */
728+ @ Deprecated
744729 public RequestTemplate body (String bodyText ) {
745730 byte [] bodyData = bodyText != null ? bodyText .getBytes (UTF_8 ) : null ;
746731 return body (bodyData , UTF_8 );
747732 }
748733
734+ /**
735+ * Set the Body for this request.
736+ *
737+ * @param body to send.
738+ * @return a RequestTemplate for chaining.
739+ */
740+ public RequestTemplate body (Request .Body body ) {
741+ this .body = body ;
742+
743+ header (CONTENT_LENGTH );
744+ if (body .length () > 0 ) {
745+ header (CONTENT_LENGTH , String .valueOf (body .length ()));
746+ }
747+
748+ return this ;
749+ }
750+
749751 /**
750752 * Charset of the Request Body, if known.
751753 *
752754 * @return the currently applied Charset.
753755 */
754- public Charset charset () {
756+ public Charset requestCharset () {
755757 return charset ;
756758 }
757759
758760 /**
759761 * The Request Body.
760762 *
761763 * @return the request body.
764+ * @deprecated replaced by {@link RequestTemplate#requestBody()}
762765 */
766+ @ Deprecated
763767 public byte [] body () {
764- return body ;
768+ return body . asBytes () ;
765769 }
766770
767771
@@ -770,11 +774,11 @@ public byte[] body() {
770774 *
771775 * @param bodyTemplate to use.
772776 * @return a RequestTemplate for chaining.
777+ * @deprecated replaced by {@link RequestTemplate#body(feign.Request.Body)}
773778 */
779+ @ Deprecated
774780 public RequestTemplate bodyTemplate (String bodyTemplate ) {
775- this .bodyTemplate = BodyTemplate .create (bodyTemplate );
776- this .charset = Util .UTF_8 ;
777- this .body = null ;
781+ this .body (Request .Body .bodyTemplate (bodyTemplate , Util .UTF_8 ));
778782 return this ;
779783 }
780784
@@ -784,7 +788,7 @@ public RequestTemplate bodyTemplate(String bodyTemplate) {
784788 * @return the unresolved body template.
785789 */
786790 public String bodyTemplate () {
787- return ( bodyTemplate != null ) ? bodyTemplate . toString () : null ;
791+ return body . bodyTemplate () ;
788792 }
789793
790794 @ Override
@@ -884,6 +888,10 @@ private SimpleImmutableEntry<String, String> splitQueryParameter(String pair) {
884888 return new SimpleImmutableEntry <>(name , value );
885889 }
886890
891+ public Request .Body requestBody () {
892+ return this .body ;
893+ }
894+
887895 /**
888896 * Factory for creating RequestTemplate.
889897 */
@@ -894,4 +902,5 @@ interface Factory {
894902 */
895903 RequestTemplate create (Object [] argv );
896904 }
905+
897906}
0 commit comments