how to PUT multiple query parameter in REST web service

45,469

Solution 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

Solution 2

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

Solution 3

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.

Share:
45,469
sudo
Author by

sudo

Updated on February 02, 2020

Comments

  • sudo
    sudo about 4 years

    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?

  • sudo
    sudo about 13 years
    i have invoked curl using " curl -X PUT localhost:8080/project/resources/user/directory/… ". But I got exception Http "500". Still have a problem. If I type is read&write , how shall i do? like " ?name=shareuser'&'type=read&write "
  • Mike Baranczak
    Mike Baranczak about 13 years
    No, read what I wrote again. Put quotes around the URL. Like this: curl 'example.com/?a=A&b=B'
  • sudo
    sudo about 13 years
    hi mike, got same error Http 500. i wrote like : curl -X PUT 'localhost:8080?a=A&b=B'
  • sudo
    sudo about 13 years
    yes I have tried, but got same Error. Maybe Coz my PUT function is conflict. I have PUT function for create container also.
  • sudo
    sudo about 13 years
    hi donal, I don't use any framework for web service. I just create in NetBean as web application and write REST web service in POJO class. This PUT method is for share function not user athentication or upload. I am new to web service and I am doing interning about storage. Supervisor ask me to finish as early as possible. So colud u give any suggestion?
  • Donal Fellows
    Donal Fellows about 13 years
    @user: You bet you are using a framework! Something is processing those annotations to turn them into code that hooks everything together. Given that you're using Netbeans, I'd guess you're probably using Jersey even if you don't know it. Multipart handling in Jersey is an extension (jersey-multipart).
  • Mike Baranczak
    Mike Baranczak about 13 years
    A 500 error is something completely different than the problem you originally reported, so you might want to start another question. Also, a 500 just means that something went wrong during request processing, so it could be anything. There should be messages in the server log when the error happens - take a look at those first.