I am trying to POST to create an image for images using their API and Play Framework WSRequest .
My code is as follows:
public static void upload( Picture picture ) throws Exception { //set file parameter - in this case the image WS.FileParam fp = new WS.FileParam( picture.asFile, "fileupload"); //set other parameters Map<String,Object> params = new HashMap<String, Object>(); params.put( "optsize", "resample" ); params.put( "rembar", "yes" ); params.put( "public", "no" ); params.put( "a_username", username ); params.put( "a_password", password ); params.put( "key", a_key ); //POST request Document doc = WS.url( "http://www.imageshack.us/upload_api.php" ) .setHeader( "Content-Type", picture.contentType ) .mimeType( "multipart/form-data" ) .params( params ) .files( fp ) .post() .getXml(); }
However, I always give the following response from the image:
Sorry, but we found that unexpected data was received. The required parameter "fileupload" is missing, or your message is not multipart / form-data.
I tried to send the file as a parameter using an array of bytes:
params.put( "fileupload", Base64.encode( picture.asBytes ) )
But it also leads to the same response from Imageshack.
It drives me crazy. Can someone point out where I'm wrong or maybe point me towards a better solution? Thanks.
Cause
After a little research, I found that I had forgotten about the important information from this question .... I am including the Google App Engine in my application.
According to the Google Groupβs Play Framework, the code associated with attaching files to a WS request when using GAE is actually just commented out. Hence the reason this just doesn't work. Thus, there is no error for you, and there is no indication as to why it does not work ... you just need to solve it.
I accepted @Gary's answer, as this is the right way to upload an image to imagehack using WS - just not when using GAE.