mailerlite API error code 422 for subscribers endpoint
Today, I was implementing the interfacing between PiCockpit.com and the Mailerlite API (which we use aus our newsletter software).
https://api.mailerlite.com/api/v2/subscribers
I got the following error message:
{"error":{"code":422,"message":"Unprocessable entity"}}
The POST request was returning 422 as status code.
Unfortunately there is little documentation about that on Mailerlite. My implementation was correct (this is Crystal Lang by the way):
get "/test" do |env|
body = "{\"email\": \"[email protected]\"}"
puts body
response = HTTP::Client.post("https://api.mailerlite.com/api/v2/subscribers",
headers: HTTP::Headers{
"X-MailerLite-ApiKey" => "MAILERLITE_APIKEY",
"Content-Type": "application/json"
},
body: body)
if response.status_code != 201
puts "newsletter_subscription :: error code #{response.status_code}"
puts response.body
else
puts "newsletter_subscription :: OK"
puts response.body
end
end
where MAILERLITE_APIKEY is the API key which you create in the backend:
https://dashboard.mailerlite.com/integrations/api
(Click on “Generate new token”, it is displayed only once to you).
How to fix 422 when calling the subscribers endpoint of the Mailerlite API
The mailerlite support gave me the solution:
certain email addresses are rejected – e.g. email addresses which are support@<domain>. This is due to the bounce rate being higher on them.
How does a successful response look like?
With a different e-mail address, the response looks like this:
{“id”:2348923942342834,”name”:””,”email”:”[email protected]”,”sent”:0,”opened”:0,”opened_rate”:0,”clicked”:0,”clicked_rate”:0,”type”:”unconfirmed”,”country_id”:””,”signup_ip”:null,”signup_timestamp”:null,”confirmation_ip”:null,”confirmation_timestamp”:null,”fields”:[{“key”:”last_name”,”value”:””,”type”:”TEXT”},{“key”:”company”,”value”:””,”type”:”TEXT”},{“key”:”country”,”value”:””,”type”:”TEXT”},{“key”:”city”,”value”:””,”type”:”TEXT”},{“key”:”phone”,”value”:””,”type”:”TEXT”},{“key”:”state”,”value”:””,”type”:”TEXT”},{“key”:”z_i_p”,”value”:””,”type”:”TEXT”}],”date_subscribe”:”2022-05-19 12:09:24″,”date_unsubscribe”:null,”date_created”:”2022-05-19 12:09:24″,”date_updated”:”2022-05-19 12:09:24″}
The response code is 201, “created”.
Hire us
If you need support for your backend or frontend development, you can hire us. Get in touch for more information.