file upload with post
henrik at witsbits.com
Thu Mar 19 04:23:38 EDT 2009
sön 2009-03-15 klockan 23:41 +0100 skrev Karl Hiramoto:
> Joe Orton wrote:
> > On Thu, Mar 12, 2009 at 04:18:26PM +0100, Karl Hiramoto wrote:
> >> Is there anyone out there that has some example code of how to do a file
> >> upload with post. I've used neon before to do some http GET, but not
> >> post.
> > If you wish to simply use a file as the POST request body, given an open
> > fd for the file, try:
> > ne_request *req = ne_request_create(sess, "POST", path);
> > ne_set_request_body_fd(req, fd, 0, <length-of-file>);
> > ret = ne_request_dispatch(req);
> > // check for errors from ret
> > ne_request_destroy(req);
> > Regards, Joe
> I'm not sure if this is what i want, maybe i didn't ask the question
> correctly. I need something that will upload to a multipart/form-data
> content type.
> One thing that was not clear to me from the man page of
> ne_set_request_body_fd() is if i would be able to set the
> multipart/form-data boundry=abcd, and how to separate the different parts.
AFAIK there is no simple interface for this in neon, so you will have to
encode the body manually. That is you will have to read the file and
create a new file where you write out the data in the format that
content-disposition: form-data; name="field1"
[data for field1]
content-disposition: form-data; name="field2"
[data for field2]
content-disposition: form-data; name="filedata"; filename="filename"
Content-Type: [mime type]
[the binary data of the file to upload]
Of course changing the names of the fields etc as your form needs. And
then you will have to POST that new file and add the header:
Content-Type: multipart/form-data, boundary=abcd
However it is not possible to add custom headers to the basic functions
so you will have to create a manual request, like this if you could hold
the whole of the new file in memory:
ne_request *req = ne_request_create (session, "POST", "[your URL]");
ne_add_request_header (req, "Content-Type:", "multipart/form-data,
ne_set_request_body_buffer (req, buffer, len-of-buffer);
int ret = ne_request_dispatch (req);
To send the file from disk use the ne_set_request_body_fd() function
instead of ne_set_request_body_buffer().
Hope this helps.
More information about the neon