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

Hello Monks, am trying to parse some json API, Here is the json
curl "https://api.zeptomail.com/v1.1/email" \ -X POST \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -H "Authorization:<SEND_MAIL_TOKEN>" \ -d '{ "bounce_address":"<BOUNCE_ADDRESS>", "from": {"address": "noreply@<DOMAIN>"}, "to": [{"email_address": {"address": "user@domain.com","name": + "John Deo"}}], "subject":"Test Email", "htmlbody":"<div><b> Test email sent successfully. </b></div>" +}'
My Try:
#!/usr/bin/perl use strict; use warnings; use HTTP::Tiny; use JSON; use Data::Dumper; print "Content-type: text/html\n\n"; my $url = "https://api.zeptomail.com/v1.1/email"; my $json = encode_json { bounce_address => 'bounce@email', from => 'noreply@email.com', to => 'receipt@email.com', subject => 'Test Email', htmlbody => '<div><b> Test email sent successfully. </b></div>', }; my $http = HTTP::Tiny->new( default_headers => { Authorization => 'Token', } ); my $response = $http->post( $url => { content => $json, headers => { 'Content-Type' => 'application/json' }, }); if ( $response->{'is_success'} ) { print Dumper( decode_json $response->{'content'} ); } else { print "$response->{'content'} $response->{'reason'}\n"; }
Am getting this Response( ERROR):
{"error":{"code":"TM_3501","details":[{"code":"GE_122","message":"Inva +lid value found","target":"from"}],"message":"Unprocessable Entity"," +request_id":"2d6f.1281848db93b53d5.m1.659b6560-d7c7-11ed-90d9-525400d +4bb1c.1876c4546b6"}}

Replies are listed 'Best First'.
Re: Problem parsing json
by NERDVANA (Priest) on Apr 11, 2023 at 09:05 UTC
    The error message (which is not from perl, its from the server you're talking to) sounds like the server didn't like your 'from' field.

    In your curl command, you wrote "from": {"address": "noreply@<DOMAIN>"}

    but in your perl, you wrote from => 'noreply@email.com'

    If curl works and the perl doesn't, I'd suggest sending the same structure as you did with curl.

      i have tried to combine the original curl and the constructed format to post via perl

      this is the original "from": {"address": "noreply@<DOMAIN>"}
      when constructed. from => 'noreply@email.com'

      the point should be how to write well constructed message to post

      because the curl message is not valid as shown

      "code":"GE_122","message":"Invalid value found","target":"from"}],"mes +sage":"Unprocessable Entity",
        If you go look at the API doc on https://www.zoho.com/zeptomail/help/api/email-sending.html (which was the first result from googling "zeptomail api") it says "From", "JSON Object", "Allowed value - A valid sender email address with "address" and "name" key-value pairs."

        So, try perl like:

        from => { address => "noreply@email.com", name => "No Reply" },

Re: Problem parsing json
by Corion (Patriarch) on Apr 11, 2023 at 06:11 UTC

    Can you describe to us where exactly your problem is?

    Is the problem that you expect success but receive an error from the API? This would be something you need to consult the documentation of your API.

    Is the problem to parse the output of the response reason? This could be solved by using the JSON module to parse the response.

Re: Problem parsing json
by soonix (Chancellor) on Apr 11, 2023 at 10:58 UTC
    After comparong the output from https://corion.net/curl2lwp.psgi with your Perl, it looks to me like there is a difference in the JSON from your curl and the JSON in your script. Especially the "from" and "to" subkeys have different datatypes.
Re: Problem parsing json
by LanX (Saint) on Apr 10, 2023 at 18:58 UTC
    Doesn't look like a Perl or JSON parsing problem to me.

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery