使用cURL在播放应用程序上发布文件

使用cURL在播放应用程序上发布文件,curl,playframework,playframework-2.0,Curl,Playframework,Playframework 2.0,我需要上传一个文件,通过命令行的播放应用程序,我已经写了。 问题是我需要一些身份验证(我使用了教程中描述的安全的机制),当我使用cURL命令时: curl -i -F name=test -F filedata=@localfile.jpg http://example.org/upload 应用程序在尝试验证用户是否具有执行上载的权限时抛出NullPointerException 我设法使用cURL登录(感谢): 我得到了答案: HTTP/1.1 303 See Other Locati

我需要上传一个文件,通过命令行的播放应用程序,我已经写了。 问题是我需要一些身份验证(我使用了教程中描述的
安全的
机制),当我使用cURL命令时:

curl -i -F name=test -F filedata=@localfile.jpg http://example.org/upload
应用程序在尝试验证用户是否具有执行上载的权限时抛出NullPointerException

我设法使用cURL登录(感谢):

我得到了答案:

 HTTP/1.1 303 See Other
 Location: /
 Set-Cookie: PLAY_SESSION="891c38b687bf198d31af51cc61647f8339d30657-email=some.email%40address.com"; Path=/; HTTPOnly
 Content-Length: 0
现在,我如何使用这个cookie发布我的文件

一些代码,自从你问起


您可以通过以下方式将从登录请求中获得的cookie保存到名为“cookie.jar”的文件中:

为了使用cookie,只需执行以下操作

 curl -b cookie.jar http://example.org/your_endpoint

添加处理请求的控制器的代码
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();
        }
    }
}
curl -c cookie.jar -i --data "email=some.email@address.com&password=somepassword"  http://example.org/login
 curl -b cookie.jar http://example.org/your_endpoint