I need to upload a file via command line on a Play application I've written.
The problem is that I need some authentication (I used the Secured
machinery described in the tutorial), and when I use the cURL command:
curl -i -F name=test -F filedata=@localfile.jpg http://example.org/upload
the application throws a NullPointerException when it tries to verify if the user has the privileges to perform the upload.
I managed to log in using cURL (thanks to this answer):
curl -i --data "email=some.email@address.com&password=somepassword" http://example.org/login
and I got the answer:
HTTP/1.1 303 See Other
Location: /
Set-Cookie: PLAY_SESSION="891c38b687bf198d31af51cc61647f8339d30657-email=some.email%40address.com"; Path=/; HTTPOnly
Content-Length: 0
Now, how can I use this cookie to post my file?
Some code, since you asked
public class Secured extends Security.Authenticator {
@Override
public String getUsername(Context ctx) {
return ctx.session().get("email");
}
@Override
public Result onUnauthorized(Context ctx) {
return redirect(routes.Application.login());
}
public static boolean isReadOnly() {
return UserType.READ_ONLY.equals(User.find.where().eq("email",Context.current().request().username()).findUnique().userType);
}
}
public class Upload extends Controller {
public static Result upload(String mode) throws IOException {
if(!Secured.isReadOnly()) {
(Handle the form to upload the file)
} else {
return controllers.Utils.generalForbidden();
}
}
}