Post file with Zinc

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Post file with Zinc

Sean P. DeNigris
Administrator
I'm want to post an xml file with Zinc:

Something like this
  curl -X POST "http://user:password@hudson.server.org/createItem?name=newjobname" --data-binary "@/path/to/newconfig.xml" -H "Content-Type: text/xml"

I see the form post methods, but how do I post a file?

Thanks,
Sean
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Post file with Zinc

Sven Van Caekenberghe
Sean,

On 08 May 2012, at 22:17, Sean P. DeNigris wrote:

> I'm want to post an xml file with Zinc:
>
> Something like this
>  curl -X POST
> "http://user:password@.../createItem?name=newjobname"
> --data-binary "@/path/to/newconfig.xml" -H "Content-Type: text/xml"
>
> I see the form post methods, but how do I post a file?
>
> Thanks,
> Sean

I am at the moment hard at work and forcing myself to stay focused on writing the Zn documentation that I keep promising but never seem to produce ;-)

Basically, it would go like this (untested):

ZnClient new
        url: 'http://hudson.server.org/createItem';
        queryAt: 'name' put: 'newjobname';
        username: 'user' password: 'password';
        entity: (ZnEntity
                        with: ('/tmp/foo.xml' asReference readStreamDo: [ :stream | stream contents ]) asString
                        type: ZnMimeType applicationXml);
        post.

You basically need to load the whole file since you have to know the length.
The #asString conversion is probably not needed (but encodings could have to be involved).
It can be done using streaming for files, as in ZnStaticFileServerDelegate>>#responseForFile: but it requires even more code.
And yes, maybe yet another convenience method like the #downloadTo: would be useful ;-)
Even guessing the mime-type from the extension is possible (as in ZnStaticFileServerDelegate>>#responseForFile:).
I'll see what I can do later.

Sven

--
Sven Van Caekenberghe
http://stfx.eu
Smalltalk is the Red Pill

Reply | Threaded
Open this post in threaded view
|

Re: Post file with Zinc

Sean P. DeNigris
Administrator
Sven Van Caekenberghe wrote
ZnClient new
...
        entity: (ZnEntity
                        with: ('/tmp/foo.xml' asReference readStreamDo: [ :stream | stream contents ]) asString
                        type: ZnMimeType applicationXml);
Thanks, Sven!!! Worked perfectly :)
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Post file with Zinc

Sven Van Caekenberghe
In reply to this post by Sven Van Caekenberghe

On 08 May 2012, at 22:56, Sven Van Caekenberghe wrote:

> And yes, maybe yet another convenience method like the #downloadTo: would be useful ;-)
> Even guessing the mime-type from the extension is possible (as in ZnStaticFileServerDelegate>>#responseForFile:).
> I'll see what I can do later.

I added a convenience method called ZnClient>>#uploadEntityFrom: so the example now becomes:

ZnClient new
        url: 'http://hudson.server.org/createItem';
        queryAt: 'name' put: 'newjobname';
        username: 'user' password: 'password';
        uploadEntityFrom: '/tmp/foo.xml';
        post.

Now, for other people who might get confused: this is *not* the same as doing a 'web style form based upload' !
This can be done with Zn as well, of course. It would look more or less like:

ZnClient new
        url: 'http://hudson.server.org/createItemForm';
        username: 'user' password: 'password';
        addPart: (ZnPart fieldNamed: 'file' fileNamed: '/tmp/foo.xml');
        post.

The last example is using a multi-part form-data entity.

Sven