Question

I need to include the content-length of a image /png file posted in an upload to a webservice.

But how do I calculate the content-length to include in the header?

Thanks.

I am submitting it using rest-client.

The webservice for the upload is Postful: and the documentation has been unclear: http://www.postful.com/developer/guide#uploading_attachments

Because I am writing the payload and headers, seems like I need to input that value.

I am also looking at postalmethods which says that the content-length is the user input:

http://postalmethods.com/method/2009-02-26/UploadFile

The files themselves are .PNG. I am going to attach them to a model using Paperclip, so will have a filepath from that.

The file that I need the content-length to post is stored as an attachment using paperclip, so the specific code generating problems is:

File.size(@postalcard.postalimage.url)
Was it helpful?

Solution

Well, you know how you're reading and posting the data, presumably - so you know how much data you're sending. That's the content length. If you're just sending it directly in binary as the body of the post, it's just the length of the file. If you're base-64 encoding it, then the content length will be the ((file length + 2) / 3) * 4. If it's going in a SOAP envelope etc, you'll need to take account of that.

One way of doing this for complicated situations is to build the entire post body in memory first, set the content length, and then just copy from memory directly to the post body.

OTHER TIPS

Well, you can use File.size(filepath) but it's unlikely that you'll need to - most libraries for making HTTP requests should do that automatically - which library are you using? (Or, what kind of webservice is it?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top