Play Framework: send image to imagehack using WS

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.

+4
source share
2 answers

I do not think you need to specify the content type or mime type directly.

I used the following code to download successfully.

 WS.FileParam fp = new WS.FileParam( new File("d:\\workspace\\ImageShackTest\\sample_picture.png"), "fileupload"); Map<String,Object> params = new HashMap<String, Object>(); params.put( "optsize", "resample" ); params.put( "rembar", "yes" ); params.put( "public", "yes" ); //params.put( "a_username", username ); //params.put( "a_password", password ); params.put( "key", API_KEY ); //POST request Document doc = WS.url( "http://www.imageshack.us/upload_api.php" ) .params( params ) .files( fp ) .post() .getXml(); 

I think that when you attach a file to the request, it automatically decides that it will be multipart / form-data.

This is my entire controller (except the API key)

 package controllers; import play.*; import play.mvc.*; import java.util.*; import models.*; import play.libs.*; import java.io.File; public class Application extends Controller { public static void index() { render(); } private static final String API_KEY = "API KEY REMOVED TO PROTECT THE INNOCENT"; public static void tryUpload() { WS.FileParam fp = new WS.FileParam( new File("d:\\workspace\\ImageShackTest\\sample_picture.png"), "fileupload"); Map<String,Object> params = new HashMap<String, Object>(); params.put( "optsize", "resample" ); params.put( "rembar", "yes" ); params.put( "public", "yes" ); params.put( "key", API_KEY ); String doc = WS.url( "http://www.imageshack.us/upload_api.php" ) .params( params ) .files( fp ) .post() .getString(); System.out.println(doc); index(); } } 

and this is application.conf file

 # This is the main configuration file for the application. # ~~~~~ application.name=ImageShackTest application.mode=dev %prod.application.mode=prod application.secret=JIVQE8y3y1lCzXRGprFJvoXBdi8Jpa8qE1U1mBIooLLOOYk5yyhAI5cxbEf4q4pl date.format=yyyy-MM-dd attachments.path=data/attachments mail.smtp=mock 

I did not make any other changes. Just scanned http: // localhost: 9000 / Application.tryUpload and could see XML success on the game console.

+3
source

The content type header is correctly configured.

Instead of this:

 .setHeader( "Content-Type", picture.contentType ) 

Try the following:

 .setHeader( "Content-Type", "multipart/form-data" ) 
0
source

Source: https://habr.com/ru/post/1388954/


All Articles