Zinc - multipart forms query

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

Zinc - multipart forms query

Ben Coman
We are trying to replicate the following in Pharo...
$ echo "hello; world" | curl -s -F "syntax=smalltalk" -F "content=<-" http://dpaste.com/api/v2/

like this...
    url := ZnClient new
       logToTranscript;
       systemPolicy;       
       beOneShot;
       url: 'http://dpaste.com/api/v2/';
       formAt: 'syntax' put: 'smalltalk';
       formAt: 'content' put: 'hello; world';
       post.

Curl produces... "hello; world" 
     http://dpaste.com/2Y15WGY  
but Zinc produces only... "hello" 
     http://dpaste.com/28E974Y

The difference is apparent on the wire...
With curl...
image.png

With Zinc...
image.png

The curl manpage says... 
    -F, --form <name=content>
    For HTTP protocol family, this lets curl emulate a filled-in form in which 
    a user has pressed the submit button. This causes curl to POST data 
    using the Content-Type multipart/form-data according to RFC 2388. 

So we've used multipart forms elsewhere already...

so I'm sure we can achieve what we want, just curious if there might be 
any convenience methods that could be used similar to the examples 
given on the rest of curl's "-F" description.  
Or if there could be?   Curl examples are often used for reference by APIs (http://dpaste.com/api/v2/)  
and a close map between curl and Zinc would be really great.

cheers -ben

P.S. I even wonder if a ZnCurl would be useful that parsed curl strings.
This would firstly provide ease of use for newcomers, 
and secondly define how to map through to native Zinc methods.
Reply | Threaded
Open this post in threaded view
|

Re: Zinc - multipart forms query

Sven Van Caekenberghe-2
Hi Ben,

Try:

ZnClient new
  systemPolicy;      
  beOneShot;
  url: 'http://dpaste.com/api/v2/';
  addPart: (ZnMimePart fieldName: 'syntax' value: 'smalltalk');
  addPart: (ZnMimePart fieldName: 'content' value: 'hello; world');
  post.

Although curl is powerful, well known and well supported, I would not say it is a great standard for interface design, it is way too complex. Zn tries to approach HTTP from an object design standpoint. You pass along a ZnMultiPartFormDataEntity object that contains ZnMimeParts as entity to your POST request.

Sven

> On 17 May 2019, at 05:26, Ben Coman <[hidden email]> wrote:
>
> We are trying to replicate the following in Pharo...
> $ echo "hello; world" | curl -s -F "syntax=smalltalk" -F "content=<-" http://dpaste.com/api/v2/
>
> like this...
>     url := ZnClient new
>        logToTranscript;
>        systemPolicy;      
>        beOneShot;
>        url: 'http://dpaste.com/api/v2/';
>        formAt: 'syntax' put: 'smalltalk';
>        formAt: 'content' put: 'hello; world';
>        post.
>
> Curl produces... "hello; world"
>      http://dpaste.com/2Y15WGY 
> but Zinc produces only... "hello"
>      http://dpaste.com/28E974Y
>
> The difference is apparent on the wire...
> With curl...
> <image.png>
>
> With Zinc...
> <image.png>
>
> The curl manpage says...
>     -F, --form <name=content>
>     For HTTP protocol family, this lets curl emulate a filled-in form in which
>     a user has pressed the submit button. This causes curl to POST data
>     using the Content-Type multipart/form-data according to RFC 2388.
>
> So we've used multipart forms elsewhere already...
> https://github.com/exercism/pharo-smalltalk/blob/4a88c397/dev/src/ExercismTools/ExercismHttpClient.class.st#L95-L107 
> https://github.com/exercism/pharo-smalltalk/blob/4a88c397/dev/src/ExercismTools/ExercismHttpClient.class.st#L213-L218 
> https://github.com/exercism/pharo-smalltalk/blob/9c6b91aaf/dev/src/ExercismSystemTests/ExercismHttpClientTest.class.st#L79-L82 
> https://github.com/exercism/pharo-smalltalk/blob/0e0bd82ed/dev/src/ExercismTools/ZnMimePart.extension.st#L4-L18 
>
> so I'm sure we can achieve what we want, just curious if there might be
> any convenience methods that could be used similar to the examples
> given on the rest of curl's "-F" description.  
> Or if there could be?   Curl examples are often used for reference by APIs (http://dpaste.com/api/v2/)  
> and a close map between curl and Zinc would be really great.
>
> cheers -ben
>
> P.S. I even wonder if a ZnCurl would be useful that parsed curl strings.
> This would firstly provide ease of use for newcomers,
> and secondly define how to map through to native Zinc methods.