0

I have webservice that I am trying to call:

The following Curl command works for that

    curl -F fa=c.apiupload -F sessiontoken=EA3237F922644115A0F7DB75D0AE388F -F destfolderid=52482BD488DB4AD6887C5C7BF47BD6FC -F filedata=@/Users/cpinera/tmp/panda2.jpg -F zip_extract=1 -F metadata=1 -F meta_img_description="This is a very nice panda" -F meta_img_keywords="panda,happy panda" http://domain.com/razuna/raz1/dam/index.cfm

but the equivalent URL that I generates as my HttpURLConnection for accessing RESTFul API does not :

 http://domain.com/razuna/raz1/dam/index.cfm?fa=c.apiupload&sessiontoken=F46D2226463C4ADE866819AACD7D2F5E&filedata=C:\JobInterview\BatchUpload\auth.xml&destfolderid=52482BD488DB4AD6887C5C7BF47BD6FC&zip_extract=1

I get this response for the REST Request:

   The content was :: <?xml version="1.0" encoding="UTF-8"?><Response><responsecode>1</responsecode><message>Upload failed This was not an uploaded form type</message></Response>

Here is the Java Code

Method that uses queries the URL:

public static String doQuery(String loginUrl) throws IOException{

    URL url = new URL(loginUrl);
    HttpURLConnection conn =
          (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("POST");

      if (conn.getResponseCode() != 200) {
        throw new IOException(conn.getResponseMessage());
      }


      InputStream is = conn.getInputStream();
      //Buffer the result into a string
      BufferedReader rd = new BufferedReader(
          new InputStreamReader(is));
      StringBuilder sb = new StringBuilder();
      String line;
      while ((line = rd.readLine()) != null) {
         line = StringEscapeUtils.unescapeHtml4(line); 
        sb.append(line);
      }

      rd.close();
      conn.disconnect();
      System.out.println("The content was :: " + sb.toString());
      return sb.toString();
}

Method that should upload:

  public static void testUpload(String seesionToken, String file ) throws IOException{

    String upload = "http://domain.com:8080/razuna/raz1/dam/index.cfm?fa=c.apiupload&sessiontoken="+seesionToken+"&filedata="+file+"&destfolderid=52482BD488DB4AD6887C5C7BF47BD6FC&zip_extract=1" ;
    System.out.println(upload);
    Authenticate.doQuery(upload);
}

After adding in doQuery: conn.setRequestProperty("Content-Type", "multipart/form-data");

And changing RESTFul URL to:

       http://domain.com:8080/razuna/raz1/dam/index.cfm?fa=c.apiupload&sessiontoken="+seesionToken+"&filedata="+files.toURI()+"&destfolderid=52482BD488DB4AD6887C5C7BF47BD6FC&zip_extract=1"

It seems to detect that URL is trying to send a File but still cannot upload file:

        The content was :: <?xml version="1.0" encoding="UTF-8"?><Response><responsecode>1</responsecode><message>Upload failed There was no appropriate FILE found in the upload</message></Response>
1
  • Based on your answers, it looks like you're actually trying to write Java code that performs a particular REST API function. However, nothing in your question indicates that, and you've not posted any code that shows what you're currently trying. Commented Mar 21, 2012 at 7:39

2 Answers 2

1

Your curl command is uploading the file @/Users/cpinera/tmp/panda2.jpg. Using the URL by itself will not, it will simply access the page, hence your error that 'this was not an uploaded form'.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response. Can you please tell me what is the correct way to pass file. Here is the link to web service / REST documentation wiki.razuna.com/display/ecp/Upload .
0

When you run curl with -F parameters, it sends an HTTP POST request with the specified name-value pairs passed in the body of the HTTP request. When you access a URL using query string parameters, an HTTP GET is performed. It seems likely that the server handles those two request methods differently.

Update, now that you've posted some Java code:

The values in the query string portion of the URL are used to convey information to the web server that will be handling the request. Of course, this remote server is not going to know what C:/path/to/your/file is, because this is a path to a file on your local machine. The proper way to send this file data is to open the file in Java, and then read it and write it to the HttpUrlConnection's output stream.

For example, see the code snippet under "Posting Content" on the Android documentation's discussion of HttpUrlConnection: http://developer.android.com/reference/java/net/HttpURLConnection.html

1 Comment

Thanks for the quick response. I tried setting to POST didnot work :( HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.