views:

186

answers:

2

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)
A: 

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.

Jon Skeet
How do I get that size...should I just use File.size?What do you mean if it's going to a SOAP envelope -- I guess that's what I'm trying to figure out the documentation (I added to my question) for the webservice wasn't clear....
Angela
@Angela: It looks like you don't have an envelope or anything similar - just the content. So just put the length of the size. File.size sounds like the right way to go; I'm not a Ruby person, so I don't know for sure.
Jon Skeet
okay, hoping that's the right way, confusing.
Angela
A: 

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?)

Gareth
Hmm...maybe it is done automatically, I am using rest-client. The webservice is Postful: and the documentation has been unclear.http://www.postful.com/developer/guideI am also looking at postalmethods which says that the content-length is the user input:http://www.postalmethods.com/method/2009-02-26/UploadFile
Angela
That method documentation doesn't seem to mention Content-length as far as I can see, just the file data itself in base64 format. Where do you think it says you need to provide the length?
Gareth
It says in the header I need to provide content-length :(
Angela