开发者

Uploading files via JSON Post request to a Web Service provided by Teambox

开发者 https://www.devze.com 2023-03-15 06:50 出处:网络
Please re开发者_如何学编程fer to(looks pretty simple documentation): https://teambox.com/api/upload

Please re开发者_如何学编程fer to(looks pretty simple documentation): https://teambox.com/api/upload

The parameters I am suppose to pass as JSON are:

{
  "page_id": 456,
  "project_id": 123,
  "name": "Name",
  "asset": "<File Data>"
}

to a URL: /api/1/uploads

My guess was that I am to pass a form's encoded request in place of File Data above. But I keep getting error: /api/1/uploads returned a response status of 422 null

So Here is my code:

A form

     <FORM  ENCTYPE="multipart/form-data" ACTION=
"up.jsp" METHOD=POST>
                <br><br><br>
          <center><table border="2" >
                    <tr><center><td colspan="2"><p align=
"center"><B>PROGRAM FOR UPLOADING THE FILE</B><center></td></tr>
                    <tr><td><b>Choose the file To Upload:</b>
</td>
                    <td><INPUT NAME="F1" TYPE="file"></td></tr>
                                        <tr><td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>
             <table>
     </center>      
     </FORM>

up.jsp

<%@ page import="java.io.*" %>
<%@ page import="com.sun.jersey.api.client.*" %>
<%@ page import="com.sun.jersey.api.client.filter.*" %>
<%@ page import="com.google.gson.*" %>
<%@ page import="OtherClasses.UploadFile" %>
<%  
         StringBuffer inputLine = new StringBuffer();
         try 
         {
            DataInputStream dis = new DataInputStream(new   DataInputStream(request.getInputStream()));
            String tmp; 
            while ((tmp = dis.readLine()) != null) {
                inputLine.append(tmp);
                //System.out.println(tmp);
            }
            dis.close();
        } 
        catch (IOException ioe) {
            System.out.println("IOException: " + ioe);
        }
        String encodedRequest = inputLine.toString();
        out.print(encodedRequest);

                Client client = Client.create(); 
        client.addFilter(new HTTPBasicAuthFilter("username", "password"));
        WebResource webResource = client.resource("http://my-server-ip:3000");

        UploadFile file = new UploadFile();
        file.setAsset(encodedRequest);
        file.setName("Afile");
        file.setPage_id("2");//404 error if page doesn't exist.
        file.setProject_id("1");

        ClientResponse r = webResource.path("/api/1/uploads").type("application/json").post(ClientResponse.class,new Gson().toJson(file));
        System.out.println(r);



%>

I keep getting response error code as 422. Please suggest something. What could I try? Thanks


What you need to do is use a file upload library (such as Commons File Upload) to extract the actual file content (as opposed to the entire request body) and then base64 encode the file into the asset field.

As an aside, the API documentation is pretty bad: they say "<File Data>" and form encoding but provide no meaningful examples. I'm pretty sure that what you've done (which is read in the entire HTTP request and save that as the asset field) will not work.

0

精彩评论

暂无评论...
验证码 换一张
取 消