Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: 400 bad request curl post

by talexb (Canon)
on Dec 09, 2018 at 14:10 UTC ( #1227006=note: print w/replies, xml ) Need Help??

in reply to 400 bad request curl post

I'm a bit late to the party, but here's my two cents: Create a useful script that lets you send stuff to the API as a testbed. Here's some code I use to talk to Freshdesk:

#!/usr/bin/perl use strict; use warnings; # 2018-0425: Test script to access the Freshdesk API. ... use LWP::UserAgent; use JSON; use Data::Dumper; use utf8; # Path to authentication module ... { my $ua = LWP::UserAgent->new; my $headers = $ua->default_headers; $ua->default_header( 'Accept', 'application/json' ); $ua->default_header( 'Accept-Charset', 'UTF-8' ); $headers->authorization_basic( $Auth::User, $Auth::Password ); binmode STDOUT, ":utf8"; foreach my $arg (@ARGV) { # 2018-0426: If it's a query, then that stuff has to be within # double quotes. The command line processing throws them out. if ( $arg =~ /^(.+query=)(.+)/ ) { $arg = join ( '', $1, '"', $2, '"' ); } my $this_url = join ( '', $Auth::Url, $arg ); print "GET $this_url ..\n"; my $response = $ua->get($this_url); if ( $response->is_success ) { my $result = decode_json( $response->content ); print Dumper ($result); if ( exists ( $result->{'description_text'} ) ) { my $as_is = $result->{'description_text'}; print "Text as-is: $as_is.\n"; } } else { print "Ugh, not a good response.\n"; print "Response:\n" . " " . $response->status_line . "\n" . " " . $response->message . "\n" . " " . $response->content . "\n"; } } }

I don't know if this is applicable to your situation, but it's a way of handling good and bad responses as a way of testing what works and what doesn't with an API. I've used this approach with Freshdesk, BigCommerce and Stripe. You'll probably find that some adjustments are necessary.

Also note that I've put my authentication (URL, user and password) into a separate module .. that's a really good idea, in case you end up copying and pasting your code on-line. That module is in another directory that's not under source control -- because there's no reason to put that kind of sensitive information into version control.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1227006]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2019-07-23 05:04 GMT
Find Nodes?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?

    Results (24 votes). Check out past polls.