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

i have the following api

curl --location 'https://api.link.io/v1/payment' \ --header 'x-api-key: {{api-key}}' \ --header 'Content-Type: application/json' \ --data '{ "price_amount": 3999.5, "price_currency": "usd", "pay_currency": "usd", "ipn_callback_url": "https://link.io", "order_id": "RGDBP-21314", "order_description": "Apple Macbook Pro 2019 x 1" }'

and am trying to parse it like this

#!/usr/bin/perl use strict; use warnings; use HTTP::Tiny; use JSON; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $url = "https://api.link.io/v1/payment"; my $json = encode_json { price_amount => '10', price_currency => 'usd', pay_currency => 'usd', ipn_callback_url => 'https://link.com', order_id => 'RGDBP-21314', order_description => 'Apple Macbook Pro 2019 x 1' }; my $http = HTTP::Tiny->new(); my $response = $http->post( $url => { content => $json, headers => { 'x-api-key' => '{{MV34WM5Y8Y}}'}, headers => { 'Content-Type' => 'application/json' } }); print "Content-type: text/html\n\n"; if ( $response->{'is_success'} ) { print (decode_json $response->{'content'} ); } else { print "$response->{'status'} $response->{'content'}\n"; }

the response am getting is 403 {"status":false,"statusCode":403,"code":"INVALID_API_KEY","message":"Invalid api key"}

but the api key is valid, i think the problem is at the way i make my request

Replies are listed 'Best First'.
Re: curl url request
by Corion (Patriarch) on May 24, 2024 at 13:23 UTC

    You should compare what curl sends with what you send.

    Invoke curl with -v -v -v to see what it sends over the wire.

    Look at that.

    Look at your script and the parameters you give it, and look at the API documentation for the API.

    When I run your code through my Curl to Perl converter, I get the following:

    #!perl use strict; use warnings; use HTTP::Tiny; my $ua = HTTP::Tiny->new( 'verify_SSL' => '1' ); my $res = $ua->request( 'POST' => 'https://api.link.io/v1/payment', { headers => { 'Accept' => '*/*', 'User-Agent' => 'curl/7.55.1', 'Content-Length' => '196', 'Content-Type' => 'application/json', 'X-Api-Key' => '{{api-key}}' }, content => "{ \x22price_amount\x22: 3999.5, \x22price_currency\x22: \x22usd\x22 +, \x22pay_currency\x22: \x22usd\x22, \x22ipn_callback_url\x22: \x22 +https://link.io\x22, \x22order_id\x22: \x22RGDBP-21314\x22, \x22ord +er_description\x22: \x22Apple Macbook Pro 2019 x 1\x22}" }, );

    Most likely, the API does not want {{ABCDEF}} but ABCDEF in its x-api-key header.

      I concur with this advise.

      I've chased this sort of thing before and it almost always comes down to some header tha nothing says is relevant.
      Typically the User-Agent, but YMMV

Re: curl url request
by hippo (Archbishop) on May 24, 2024 at 13:36 UTC
    headers => { 'x-api-key' => '{{MV34WM5Y8Y}}'},

    If that's your actual API key, you probably want to revoke it. ;-)


    🦛

Re: curl url request
by sectokia (Friar) on Jun 04, 2024 at 04:54 UTC

    The double {{ }} brackets are probably not meant to be literal.

    You probably just need to do: headers => { 'x-api-key' => 'MV34WM5Y8Y'},