changeset 1363:1a7b8e38921a

add url post content
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 May 2019 15:11:51 -0600
parents 77f2d091f17f
children 45363886f256
files src/luan/modules/url/LuanUrl.java
diffstat 1 files changed, 19 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
diff -r 77f2d091f17f -r 1a7b8e38921a src/luan/modules/url/LuanUrl.java
--- a/src/luan/modules/url/LuanUrl.java	Sun May 26 12:42:08 2019 -0600
+++ b/src/luan/modules/url/LuanUrl.java	Wed May 29 15:11:51 2019 -0600
@@ -33,7 +33,8 @@
 	public final URL url;
 	private Method method = Method.GET;
 	private final Map<String,Object> headers = new HashMap<String,Object>();
-	private String content = "";
+	private static final byte[] NO_CONTENT = new byte[0];
+	private byte[] content = NO_CONTENT;
 	private MultipartClient multipart = null;
 	private int timeout = 0;
 	private String authUsername = null;
@@ -92,10 +93,23 @@
 			}
 			Map params = getMap(map,"parameters");
 			String enctype = getString(map,"enctype");
+			Object content = map.remove("content");
+			if( content != null ) {
+				if( this.method != Method.POST )
+					throw new LuanException( "content can only be used with POST" );
+				if( params != null )
+					throw new LuanException( "content cannot be used with parameters" );
+				if( content instanceof String ) {
+					this.content = ((String)content).getBytes();
+				} else if( content instanceof byte[] ) {
+					this.content = (byte[])content;
+				} else
+					throw new LuanException( "content must be String or byte[]" );
+			}
 			if( enctype != null ) {
 				if( !enctype.equals("multipart/form-data") )
 					throw new LuanException( "unrecognized enctype: "+enctype );
-				if( this.method!=Method.POST )
+				if( this.method != Method.POST )
 					throw new LuanException( "multipart/form-data can only be used with POST" );
 				if( params==null )
 					throw new LuanException( "multipart/form-data requires parameters" );
@@ -128,7 +142,7 @@
 					}
 				}
 				if( this.method==Method.POST ) {
-					content = sb.toString();
+					this.content = sb.toString().getBytes();
 				} else {
 					String urlS = url.toString();
 					if( urlS.indexOf('?') == -1 ) {
@@ -256,10 +270,9 @@
 		if( multipart != null ) {
 			out = multipart.write(httpCon);
 		} else {
-			byte[] post = content.getBytes();
-//			httpCon.setRequestProperty("Content-Length",Integer.toString(post.length));
+//			httpCon.setRequestProperty("Content-Length",Integer.toString(content.length));
 			out = httpCon.getOutputStream();
-			out.write(post);
+			out.write(content);
 		}
 		out.flush();
 		try {