Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

LWP::UserAgent POST example

by ssara (Acolyte)
on Jan 19, 2018 at 17:44 UTC ( [id://1207548]=perlquestion: print w/replies, xml ) Need Help??

ssara has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to use POST method to send information to an API.Please can you provide me an example on how to use it.

I would like to call the below api which requires following inputs:

URI: https://www.cryptopia.co.nz/api/SubmitTrade

Input Parameters are:-

Market: The market symbol of the trade e.g. 'DOT/BTC' (not required if 'TradePairId' supplied)

TradePairId: The Cryptopia tradepair identifier of trade e.g. '100' (not required if 'Market' supplied)

Type: the type of trade e.g. 'Buy' or 'Sell'

Rate: the rate or price to pay for the coins e.g. 0.00000034

Amount: the amount of coins to buy e.g. 123.00000000

Please can you tell me how I can call this api from perl ?

Thank You in advance!!

Replies are listed 'Best First'.
Re: LWP::UserAgent POST example
by Discipulus (Canon) on Jan 19, 2018 at 17:58 UTC
    Hello ssara and welcome to the monastery and to the wonderful world of Perl.

    You need to pass fields as an anonymous array after the url, like:

    use HTTP::Request::Common qw(POST); use LWP::UserAgent; $ua = LWP::UserAgent->new; my $req = POST 'https://rt.cpan.org/Public/Dist/Display.html', [ Status => 'Active', Name => 'libwww-perl' ]; print $ua->request($req)->as_string;

    See POST at lwpcook for the example.

    I think you can also consider WWW::Mechanize to issue your POST

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: LWP::UserAgent POST example
by 1nickt (Canon) on Jan 19, 2018 at 17:55 UTC

    Hi, start with the LWP::UserAgent doc for POST requests and follow the links shown to learn how to build your request with the data needed by the API.

    Great that your project has advanced to the point of making a trade. Be careful! You might want to practise with some generic POST requests to a less consequential endpoint until you get the hang of it.

    Hope this helps!


    The way forward always starts with a minimal test.

      FWIW I second caution. I've seen bad trading bots go rogue.

      Thank You for your reply. Unfortunately I read that link which you sent but was not able to get the output. I am not able to understand how do I request the structure:

      Request Structure:

      REQUEST_SIGNATURE: API_KEY + "POST" + URI + NONCE + HASHED_POST_PARAMS

      API_KEY: Your Cryptopia api key

      URI: the request uri. e.g. https://www.cryptopia.co.nz/Api/SubmitTrade

      HASHED_POST_PARAMS: Base64 encoded MD5 hash of the post parameters

      NONCE: unique indicator for each request.

      So question is how do I join this api https://www.cryptopia.co.nz/api/SubmitTrade and pass the arguments to it

      Any help would be really appreciated.Thank You in advance!!

        Same deal as always: show your code so far, errors/warnings, expected output.


        The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1207548]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found