CurtTilmes / raku-libcurl

A Raku interface to libcurl.
Other
19 stars 9 forks source link

post vs put #11

Open ufobat opened 6 years ago

ufobat commented 6 years ago

Hi,

I am using this module to talk to a REST API. Some API endpoints are available with the PUT method, some are available with the POST method. While talking to the REST API the content to be sent is in a variable, for both HTTP methods.

I wanted to know if you have any suggestions to make it more easy for me, or maybe if you considder making POST and PUT more similar?

https://github.com/CurtTilmes/perl6-libcurl/blob/master/lib/LibCurl/HTTP.pm6#L29-L38

CurtTilmes commented 6 years ago

I'm not quite sure what you are asking for. The current code follows the libcurl model.

Could you provide an example of how you currently call it and how you would like to call it? Or better yet, a suggested patch?

ufobat commented 6 years ago

I need to say that i'm unfortunately not familiar with libcurl; that's why i am asking on your optionion before making a nonsense pull request.

What I want to point out is that the signatures (or semantics) for the 2 methods, PUT and POST, are different.

When I am using curl on the command line put vs post looks like this:

$ curl -X POST -d '{"json": "yeah"}' http://example.com
$ curl -X PUT  -d '{"json": "yeah"}' http://example.com

I am looking for the same interchangeability in this library. What I want to do is sending $content sometimes via put and sometimes via post. If I understood it corretly, in the current version it's just possible to send $content via POST and the content of a file ($filename) via PUT.

CurtTilmes commented 6 years ago

libcurl uses some basic conventions.

postfields implies POST, and sets the type to application/x-www-form-urlencoded but you are free to override it and say to use PUT anyway.

likewise upload implies PUT, but you can override that too.

You can still just call setopt with the right stuff to make it act the way you want it to.

$http.setopt(URL => 'http://example.com',
             postfields => $content,
             customrequest => 'PUT')
      .perform;

or

$http.setopt(URL => 'http://example.com',
             upload => $filename,
             customrequest => 'POST')
      .perform;

If you can come up with a good calling sequence, we could make LibCurl::HTTP multi methods for PUT and POST that could set them for you if that would help. Patches welcome!