how to PUT multiple query parameter in REST web service

▼魔方 西西 提交于 2019-12-12 11:43:08

问题


Do anyone know how to PUT multiple query parameter in REST web service? I have write using java.My curl sample is like this:

curl -X PUT http://localhost:8080/project/resources/user/directory/sub-directory?name=shareuser&type=Read -v

My program is :

@PUT
@Path("{user}/{directory:.+}")
public Response doshare(@PathParam("user")String name,
        @PathParam("directory")String dir,
        @QueryParam("name")String sharename,
        @QueryParam("type")String type){
    mongoDAOImpl impl=new mongoDAOImpl();
    Mongo mongo=impl.getConnection("127.0.0.1","27017");
    DB db=impl.getDataBase(mongo,"public");
    DBCollection coll=impl.getColl(db,name);
    DBCollection coll2=impl.getColl(db,"sharedb");
    shareDTO sharedto=new shareDTO();
    String authority=type.toLowerCase();
    if(authority.equals("rd")){
        sharedto.setAuthority("4");
    }else if(authority.equals("rw")){
        sharedto.setAuthority("12");
    }
    sharedto.setTargetuser(sharename);
    sharedto.setRealURI("/home/public/"+name+"/"+dir);
    sharedto.setIdentifier(name);
    sharedto.setParentURI("/home/public/"+sharename);
    boolean bool = false;
    sharefun=new sharefunction();
    if(sharefun.checksubfoldershared(coll, coll2, sharedto)){
        bool=sharefun.sharefiles(coll, coll2, sharedto);
    }else{
        System.out.println("Error");
    }
    // ...

But I only get the name query parameter.How to get or how to type in curl command in order to get all query parameter?


回答1:


Your code is fine - the problem is with the way you're invoking curl. When passing a URL to curl that contains a '&', you have to put quotes around the URL. Otherwise, the shell will interpret the stuff after the '&' as a separate command.

EDIT: My text is getting munged when I submit it as a comment. Here's what you need to do:

curl -X PUT 'http://localhost:8080/project/resources/user/directory/sub-directory?name=shareuser&type=Read' -v



回答2:


You could try handling this all as a multipart upload (the server-side details aren't standardized and you didn't say what framework you're using so I can't give more clues) but I question why you're having the user and permissions set (via any parameters) in the first place. Surely you'd be better off deriving the user to store as through inspecting the user's login credentials and using a default set of permissions that they can change later on? Nearly as cheap, and enormously simpler.




回答3:


Have you tried using the -d option? e.g.

curl -X PUT -d "name=shareuser&type=Read" http://locahost:8080/project/resources/user/directory/sub-directory -v


来源:https://stackoverflow.com/questions/5785610/how-to-put-multiple-query-parameter-in-rest-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!