I'm trying to update a subscription in Stripe.

This involves calling an API with an authorisation header using POST. If the payload is empty, the API returns a JSON object representing the existing subscription. If there is subscription data in the payload, Stripe attempts to update the subscription and returns the complete subscription object. Pretty straightforward and it all works fine. Until I need to read the existing subscription object and then update it.

I have hit a problem and I can't think of how to debug it further!
Here is the minimum code to demonstrate the problem:

#!/usr/bin/perl -T use CGI::Carp qw(fatalsToBrowser); use FindBin qw($RealBin); my $safepath; BEGIN { if ($RealBin =~ m!^(/home/...path.../(test|uk)/www)!) { $safepath = "$1/../lib"; } else { die "Illegal use of software - visit www.way-finder.uk to use +this site"; } } use lib "$safepath"; use Site::Variables; use HTTP::Tiny; use JSON; use Data::Dumper; use strict; use warnings; my $http = HTTP::Tiny->new; my $headers = { 'headers' => { 'Authorization' => 'Bearer ' . $Site::Variables::stripe_secret +, }, 'agent' => 'Wayfinder/v3.0', }; my $sub_id = 'sub_xxxxxxxxxxx'; # This line is the culprit... my $res = $http->post_form("https://api.stripe.com/v1/subscriptions/$s +ub_id", {}, $headers); my $payload = decode_json($res->{'content'}); my $subscription = { 'items[0][id]' => 'x', 'items[0][price]' => 'some price', }; my $response = $http->post_form("https://api.stripe.com/v1/subscriptio +ns/$sub_id", $subscription, $headers); print "Content-type: text/plain\n\n"; print Dumper $response;
With the code as it is, I get an error from Stripe that I have not supplied an API key. The key is in the $headers variable. If I take out the first call to Stripe, the one with the empty payload, then the second one succeeds * so the API key is working fine in this case. But as soon as I make two calls, it fails.

Things I've tried but haven't helped:

Any ideas what I can try to solve this problem?

It is as if HTTP::Tiny doesn't like making consecutive POSTs but I cannot find anything in the documentation about this.


* - Without the first call, the second call to Stripe gives an error because I haven't got the parameters right. But it doesn't complain about there not not being an API key


In reply to HTTP::Tiny losing headers for Stripe by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.