how to PUT multiple query parameter in REST web service

sudo picture sudo · Apr 26, 2011 · Viewed 44.1k times · Source

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?

Answer

Mike Baranczak picture Mike Baranczak · Apr 26, 2011

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