1
votes

What is the equivalent of httpclient code for the following CURL command

 curl -v -u username:password-XPUT -H "Content-type: text/plain" -d "E:/path_to_shapefile/shapefiles/" "http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/scenario2373/external.shp?configure=all"

The CURL command works fine. I have limited knowledge of httpclient, however, adapting similar code, following is my attempt:

    import org.apache.http.client.fluent.*;

    public class QuickStart {
        public static void main(String[] args) throws Exception {  
            Executor executor = Executor.newInstance()
                    .auth("username", "password")
                    .authPreemptive("172.16.17.86:9090");
            // Line below does not compile
            String response = executor.execute(Request.Put("E:/path_to_shapefile/shapefiles/" 
     "http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/scenario2373/external.shp?configure=all"))                                
                     .returnResponse()
                     .toString();
             System.out.println(response);
        }
    }

This code above does not compile since I do not know how to encode two urls in the same request as in the CURL command. A fix to the above code or a new approach would be appreciated.

Thanks in advance.

2

2 Answers

0
votes
import org.apache.http.client.fluent.*;
import org.apache.http.entity.ContentType;

public class QuickStart {
    public static void main(String[] args) throws Exception {  
        Executor executor = Executor.newInstance()
                .auth("admin", "geoserver")
                .authPreemptive("172.16.17.86:9090");       
        String response = executor.execute(Request.Put("http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/scenario2373/external.shp?configure=all")
                .bodyString("E:\\Tomcat\\apache-tomcat-8.5.37\\webapps\\geoserver\\data\\data\\IDIRA6\\scenario2373\\", ContentType.create("text/plain")))
                 .returnResponse()
                 .toString();
         System.out.println(response);
    }
}
0
votes

Thanks for the answer. I replaced bodyFile by bodyString and that worked.

import org.apache.http.client.fluent.*;
import org.apache.http.entity.ContentType;

public class QuickStart {
    public static void main(String[] args) throws Exception {  
        Executor executor = Executor.newInstance()
                .auth("admin", "geoserver")
                .authPreemptive("172.16.17.86:9090");       
        String response = executor.execute(Request.Put("http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/scenario2373/external.shp?configure=all")
                .bodyString("E:\\Tomcat\\apache-tomcat-8.5.37\\webapps\\geoserver\\data\\data\\IDIRA6\\scenario2373\\", ContentType.create("text/plain")))
                 .returnResponse()
                 .toString();
         System.out.println(response);
    }
}