×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    43

    Http Post, can't upload an image to Imgur service.

    Anyone had any experience using the Imgur API? I've been trying to figure this out for a day or two now to no avail.

    When I tried uploading the image, it returns a "Bad Request", I've replaced my actual client ID to "MyClientID",
    so If you'd like to help, you'd need to get out of your way and register w/ the service, http://api.imgur.com/, I'm really sorry for the extra step, I don't think there's an option to reset my client ID & key hence the omission of the client ID. Thanks in advance for any help.

    Code:
     public void test(byte[] filebytes) 
    			 throws java.io.IOException {
    			
    			   String url = "https://api.imgur.com/3/upload.json"; 
    			   byte[] data = null;
    			   InputStream istrm = null;
    
    			   HttpConnection http = (HttpConnection)Connector.open(url);
    			   http.setRequestMethod(HttpConnection.POST);
    
    			  
    			   http.setRequestProperty("Content-Type", "application/octet-stream");
    			   http.setRequestProperty("Authorization", "Client-ID MyClientID");
    			   
    			   
    			   
    			   String msg = "key=7a846208a0c3cd7dca29ab8b5828cd35e15876e2&type=binary&name=test&title=test&image="+filebytes;
    			   String response;
    			   System.out.println("file bytes size: "+filebytes.length);
    			  
    			   OutputStream out = http.openOutputStream();
    			   out.write(msg.getBytes());
    			   out.flush();
    
    			   if (http.getResponseCode() == HttpConnection.HTTP_OK) {
    			     int len = (int)http.getLength();
    			     istrm = http.openInputStream();
    			     if (istrm == null) {
    			       System.out.println("Cannot open stream - aborting");
    			       throw new IOException("Cannot open HTTP InputStream, aborting");
    			     }
    			     if (len != -1) {
    			       data = new byte[len];
    			       int bytesRead = istrm.read(data);
    			      
    			     } else {
    			     ByteArrayOutputStream bo = new ByteArrayOutputStream();
    			       int ch;
    			       int count = 0;
    			       
    			       while ((ch = istrm.read()) != -1) {
    			         bo.write(ch);
    			         count++;
    			       }
    			       data = bo.toByteArray();
    			       bo.close();
    			      
    			     }
    			     response = new String(data);
    			     
    			   } else {
    			    
    			     response = null;
    			     System.out.println("failed: "+http.getResponseMessage());
    			   }
    			   
    			   http.close();
    			 }

  2. #2
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    260

    Re: Http Post, can't upload an image to Imgur service.

    Never having used Imgur API and not knowing anything about it, there seem to be at least two problems in the code:

    1) String msg = "key=..."+filebytes does not append the bytes to the string, but instead appends just a toString of the filebytes array, that might be something like "[B@12345678"

    2) Even without problem 1), it looks wrong to just append binary data (the byte array) to a HTTP POST content string (something=1&somethingelse=2...). Are you sure you don't need to encode the file bytes somehow first, or have you already done it before calling this function?

    If you're using a Nokia emulator, you can go to Tools/Diagnostics/Traffic to see the lengths, headers and contents of HTTP requests. If fixing the above things doesn't help, you might want to compare your upload request for an image to that of another application that can successfully upload the image.
    Last edited by TongMuan; 2013-07-25 at 11:54.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    43

    Re: Http Post, can't upload an image to Imgur service.

    Hello. yes. Since posting this yesterday, I've tried encoding it in base64, the problem is, the Image is coming from the camera. when encoding that, it'll throw out of memory exception, Is there a common technique where you could base64 encode camera images without hitting the memory limit?

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    260

    Re: Http Post, can't upload an image to Imgur service.

    You could try to make the image file size smaller by using a lower resolution, but a better way to avoid memory problems is to do the Base64 encoding of the image bytes straight to the request's OutputStream - that way you won't make unnecessary copies of the image data in the memory. Not sure how you currently do the encoding, but I think at least Bouncy Castle supports encoding to a stream out of the box, and it's not difficult to write your own encoder either.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    43

    Re: Http Post, can't upload an image to Imgur service.

    Thanks, Scaling down the image by half still reaches the memory limit. Yes, Bouncy Castle that's exactly what i'm currently using to attempt to encode the Image data, I see the method where you could pass an OutputStream object. not really sure how to use this. plus I couldn't find any decent docs on how to do multipart upload with the service. anyhow I'll delve into this later.

    Thanks again.

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    260

    Re: Http Post, can't upload an image to Imgur service.

    Instead of scaling, I was thinking about simply requesting a lower resolution image (640x480 or 320x240?) from the camera. But in any case, encoding the image straight to the output stream probably makes sense.

    I'm not 100% sure if or how writing to stream on Bouncy Castle works as I've never used it, but whatever encoder you use, you would do is first write to the output stream everything up to the image data itself,
    eg.
    Code:
    String msg = "key=7a846208a0c3cd7dca29ab8b5828cd35e15876e2&type=binary&name=test&title=test&image=";
    out.write(msg.getBytes());
    and then ask the encoder to encode your image to the same stream:

    Code:
    Base64.encode(fileBytes, out);

Similar Threads

  1. How to upload image on server using HTTP Post.....??
    By pranay_preet in forum Symbian Networking & Messaging (Closed)
    Replies: 11
    Last Post: 2012-02-15, 07:43
  2. HTTP Post - audio upload
    By em75dl07 in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2010-02-15, 14:25
  3. HTTP Post Upload question
    By joseph.m in forum Symbian
    Replies: 1
    Last Post: 2009-06-04, 09:17
  4. File upload .....HTTP /Post
    By Mdhaval in forum Symbian
    Replies: 2
    Last Post: 2008-04-12, 15:45
  5. Upload image to server using HTTP POST
    By felixksp in forum Symbian
    Replies: 5
    Last Post: 2007-10-11, 02:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×