static String myApiKey="fa0499d1-ffda-4590-82b3-4afdb9c91576";
static String uploadFileName="/home/ilias/2013/05/csv/2013_05_15.csv"; public static void uploadFile()
{ HttpClient httpclient = new DefaultHttpClient();
Date now=new Date(); File file = new File(uploadFileName); httpclient = new DefaultHttpClient(); try { SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyyMMMddHHmmss"); dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT")); String date=dateFormatGmt.format(new Date()); /* First we specify the data */ HttpEntity reqEntity = MultipartEntityBuilder.create() .addTextBody("package_id","test2") .addTextBody("url",page) .setCharset(Charset.forName("utf-8")) .addTextBody("file", date +"/"+uploadFileName) .build(); /* Set up the headers */ HttpPost postRequest = new HttpPost(page+"/api/action/resource_create/"); postRequest.setEntity(reqEntity); postRequest.setHeader("X-CKAN-API-Key", myApiKey); /* execute */ System.out.println("request2: "+postRequest.toString()+"\n"+postRequest.getRequestLine().toString()); Header[]a=postRequest.getAllHeaders(); HttpResponse response = httpclient.execute(postRequest); /* get respond */ int statusCode = response.getStatusLine().getStatusCode(); BufferedReader br = new BufferedReader( new InputStreamReader((response.getEntity().getContent()))); String line; while ((line = br.readLine()) != null) { System.out.println("__"+line); } if(statusCode!=200){ System.out.println("statusCode ==" +statusCode); } }catch (IOException ioe) { System.out.println(ioe); } finally { httpclient.getConnectionManager().shutdown(); } /*Upload the file */ file = new File(uploadFileName); httpclient = new DefaultHttpClient(); try { FileBody bin = new FileBody(file,ContentType.TEXT_PLAIN,now.toString()+file.getName()); /* add the data */ HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("file", bin) .addTextBody("key", now.toString()+file.getName()) .build(); HttpPost postRequest = new HttpPost(page+"/api/action/resource_create/"); postRequest.setEntity(reqEntity); postRequest.setHeader("X-CKAN-API-Key", myApiKey); HttpResponse response = httpclient.execute(postRequest); int statusCode = response.getStatusLine().getStatusCode(); BufferedReader br = new BufferedReader( new InputStreamReader((response.getEntity().getContent()))); String line; while ((line = br.readLine()) != null) { System.out.println("+"+line); } if(statusCode!=200){ System.out.println("statusCode =!=" +statusCode); } }catch (IOException ioe) { System.out.println(ioe); } finally { httpclient.getConnectionManager().shutdown(); } }
And the error i was getting is a 301 Error
I also tried this, using httpGetURIBuilder builder = new URIBuilder(); builder.setScheme("http").setHost("192.168.1.1:5000").setPath("/api/action/resource_create/") .setCharset(Charset.forName("utf-8")) .setParameter("package_id", "test2") .setParameter("url", "http://www.example.com"); URI uri = builder.build(); HttpGet postRequest = new HttpGet(uri); postRequest.setHeader("X-CKAN-API-Key", myApiKey);
Which returned a more understandable error of
"Bad request - JSON Error: No request body data"
Any help/guideline is appreciated
--
You received this message because you are subscribed to the Google Groups "CKAN Global User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ckan-global-user-...@googlegroups.com.
To post to this group, send email to ckan-global...@googlegroups.com.
Visit this group at http://groups.google.com/group/ckan-global-user-group.
To view this discussion on the web, visit https://groups.google.com/d/msgid/ckan-global-user-group/921a86fd-38e1-4701-97d7-8ae1256fb7b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to ckan-global-user-group+unsub...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to ckan-global-user-...@googlegroups.com.
To post to this group, send email to ckan-global...@googlegroups.com.
Visit this group at http://groups.google.com/group/ckan-global-user-group.
To view this discussion on the web, visit https://groups.google.com/d/msgid/ckan-global-user-group/921a86fd-38e1-4701-97d7-8ae1256fb7b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "CKAN Global User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ckan-global-user-...@googlegroups.com.
To post to this group, send email to ckan-global...@googlegroups.com.
Visit this group at http://groups.google.com/group/ckan-global-user-group.
To view this discussion on the web, visit https://groups.google.com/d/msgid/ckan-global-user-group/50232711-f7a4-4b81-975d-d9dcbafbedda%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to ckan-global-user-group+unsub...@googlegroups.com.
To post to this group, send email to ckan-global...@googlegroups.com.
Visit this group at http://groups.google.com/group/ckan-global-user-group.
To view this discussion on the web, visit https://groups.google.com/d/msgid/ckan-global-user-group/921a86fd-38e1-4701-97d7-8ae1256fb7b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "CKAN Global User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ckan-global-user-group+unsub...@googlegroups.com.
To post to this group, send email to ckan-global...@googlegroups.com.
Visit this group at http://groups.google.com/group/ckan-global-user-group.