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

时间:2014-09-30 08:21:07

标签: curl playframework playframework-2.0

我需要在我编写的Play应用程序上通过命令行上传文件。 问题是我需要一些身份验证(我使用了教程中描述的Secured机制),当我使用cURL命令时:

curl -i -F name=test -F filedata=@localfile.jpg http://example.org/upload

当应用程序尝试验证用户是否具有执行上载的权限时,会抛出NullPointerException。

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

curl -i --data "email=some.email@address.com&password=somepassword"  http://example.org/login

我得到了答案:

 HTTP/1.1 303 See Other
 Location: /
 Set-Cookie: PLAY_SESSION="891c38b687bf198d31af51cc61647f8339d30657-email=some.email%40address.com"; Path=/; HTTPOnly
 Content-Length: 0

现在,我如何使用此cookie发布我的文件?

部分代码,因为您提出了

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();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

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

curl -c cookie.jar -i --data "email=some.email@address.com&password=somepassword"  http://example.org/login

要使用cookie,只需执行

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