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

Last week I came to you looking for some guidance on how to approach an integration project between a help desk and a project management tool. I've sorted quite a number of obstacles since the help desk has a very limited amount of modules available and it is not possible to install any more of them, overall the tools at my disposal are far from ideal. With your advises and what was available I came up with the following request.

#Perl data structure containing the values we need to pass over to ]p +o[. my $rt_ticket = { company_name => 'Test', #ticket => $ticket, #requestor => $email_address, #domain => $email_domain, #SLA => $SLA, }; #Creation of JSON object and encoding of our Perl data into a JSON str +ing. my $json = JSON->new(); my $ticket_json = $json->encode($rt_ticket); #POST request containing our JSON string. my $url = 'http://<server_ip>/intranet-rest/im_company'; my $par_user = '<username>'; my $par_pass = '<password>'; my $ua = LWP::UserAgent->new(); require HTTP::Request; my $req = HTTP::Request->new( POST=> $url ); $req->header('Content-Type' => 'application/json'); $req->content( $ticket_json ); $req->authorization_basic( $par_user,$par_pass ); my $result = $ua->request( $req ); if ($result->is_success) { $RT::Logger->info('OK'); } else { $RT::Logger->warning($result->status_line);

Problem is the external REST API isn't getting the format right, and is trying to format my allegedly JSON context as if it was XML. As shown in the following error log:

Notice: im_rest_parse_xml_json_content: going to parse xml conten +t={"company_name":"Test"} Notice: im_rest_error: http_status=406, format=xml, message=Unable + to parse XML: 'error "not well-formed (invalid token)" at line 1 cha +racter 0

When I inspect the POST request sent by this script from my browser the content type is shown as:

 Content-Type: multipart/form-data; boundary=…51082146824528294102336900843

I've tried several different ways of declaring my HTTP request and of encoding the content. Perhaps there is an error in my code that the help desk's compiler isn't getting but you guys might. Maybe an easier way to sort this would be trying to encode my content into XML, since it seems that although the API is prepared to decode JSON, the default format is XML.

Replies are listed 'Best First'.
Re: Problem with content format in HTTP Request.
by NetWallah (Canon) on Apr 30, 2020 at 19:52 UTC
    Perhaps your internal REST server is ignoring the json specification, and expecting only XML.

    You can check for this in the web server log, or ask your administrator.

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

Re: Problem with content format in HTTP Request.
by jo37 (Curate) on Apr 30, 2020 at 17:20 UTC

    Are you able to create a working request with Postman? This would be a good starting point for a Perl implementation.

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$