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.
|
|---|