how can use purely java.net attach file task using asana api?
specifically, how make formed http post multipart/form-data encoded request asana api in order attach file task?
the asana api expects attachments uploaded tasks via http post request multipart/form-data encoded file:
https://asana.com/developers/api-reference/attachments#upload
as explained in this answer regarding http spec on line breaks, ensure using \r\n
line breaks java convert println()
methods platform dependent line.separator
, asana servers may not tolerate poorly formed line breaks.
a formed multipart/form-data post request this:
authorization: basic <base64_encoded_auth> content-type: multipart/form-data; boundary=14d07d7cbcf user-agent: java/1.7.0_76 host: localhost:8080 accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 connection: keep-alive content-length: 141 --14d07d7cbcf content-disposition: form-data; name="file"; filename="example.txt" content-type: text/plain file --14d07d7cbcf--
this implementation in java purely uses java.net classes manually construct formed multipart/form-data encoded post request asana attachments api:
import org.apache.commons.codec.binary.base64; import java.io.*; import java.net.httpurlconnection; import java.net.url; public class attachfiletotask { // use http compliant line feeds in request. // note java println() methods may use platform dependent line feeds. private static string line_feed = "\r\n"; public static void main(string[] args) throws exception { // task attachments endpoint string url = "https://app.asana.com/api/1.0/tasks/<task_id>/attachments"; file thefile = new file("/path/to/file.txt"); httpurlconnection connection = (httpurlconnection) new url(url).openconnection(); // set basic auth header string apikey = "<api_key>" + ":"; string basicauth = "basic " + new string(new base64().encode(apikey.getbytes())); connection.setrequestproperty("authorization", basicauth); // indicate post request connection.setdooutput(true); // unique boundary use multipart/form-data string boundary = long.tohexstring(system.currenttimemillis()); // construct body of request connection.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary); printwriter writer = null; try { writer = new printwriter(new outputstreamwriter(connection.getoutputstream(), "utf-8")); string filename = thefile.getname(); writer.append("--" + boundary).append(line_feed); writer.append("content-disposition: form-data; name=\"file\"; filename=\"" + filename + "\"").append(line_feed); writer.append("content-type: text/plain").append(line_feed); writer.append(line_feed); bufferedreader reader = null; try { reader = new bufferedreader(new inputstreamreader(new fileinputstream(thefile), "utf-8")); (string line; (line = reader.readline()) != null; ) { writer.append(line).append(line_feed); } } { if (reader != null) try { reader.close(); } catch (ioexception logorignore) { } } writer.append(line_feed); writer.append("--" + boundary + "--").append(line_feed); writer.append(line_feed); writer.flush(); writer.close(); } catch (exception e) { system.out.append("exception writing file" + e); } { if (writer != null) writer.close(); } system.out.println(connection.getresponsecode()); // should 200 system.out.println(connection.getresponsemessage()); } }
please note basic authentication using api key discouraged outside of personal use or development of utility scripts. when deploying production applications multiple users, please use asana connect (oauth 2.0)