mailjet/mailjet-apiv3-java

Mailjet client http 400 bad request

nureynisow opened this issue · 2 comments

Hello,

i'm using the mailjet java client to send email. I got HTTP 400 BAD REQUEST without further information. Below all the details :
Note that when i use postman to perform the same payload it works.

Code

package io.murid.mouridpro.core.services;

import com.mailjet.client.MailjetClient;
import com.mailjet.client.MailjetRequest;
import com.mailjet.client.MailjetResponse;
import com.mailjet.client.errors.MailjetException;
import com.mailjet.client.errors.MailjetSocketTimeoutException;
import com.mailjet.client.resource.Emailv31;
import io.murid.mouridpro.config.ApplicationProperties;
import io.murid.mouridpro.core.models.MailSendRequest;
import io.murid.mouridpro.core.models.MailSendResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.Collection;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class MailServiceImpl implements MailService {


	private final MailjetClient mailjetClient;
	private final ApplicationProperties applicationProperties;

	@Override
	public Mono<MailSendResponse> send(MailSendRequest mailSendRequest) {
		if(ObjectUtils.isEmpty(mailSendRequest.getFrom())){
			mailSendRequest.setFrom(MailSendRequest.Address.builder()
							               .email(applicationProperties.getMail().getFrom())
							               .name(applicationProperties.getMail().getFromLabel())
			                                               .build());
		}
		buildRecipientArray(mailSendRequest.getTo());
		MailjetRequest request = new MailjetRequest(Emailv31.resource)
						.property(Emailv31.MESSAGES, new JSONArray()
										.put(new JSONObject()
														.put(Emailv31.Message.FROM, buildContactJSON(mailSendRequest.getFrom()))
														.put(Emailv31.Message.TO, buildRecipientArray(mailSendRequest.getTo()))
														.put(Emailv31.Message.SUBJECT, mailSendRequest.getSubject())
														.put(Emailv31.Message.TEXTPART, mailSendRequest.getSubject())
														.put(Emailv31.Message.HTMLPART, "<h3>Dear passenger 1, welcome to <a href='https://www.mailjet.com/'>Mailjet</a>!</h3><br />May the delivery force be with you!")
														.put(Emailv31.Message.CUSTOMID, "AppGettingStartedTest")));
		try {
			log.debug("mailjet request : {}", request);
			final MailjetResponse mailJetRestponse = mailjetClient.post(request);
			log.debug("{}",mailJetRestponse);
		} catch (MailjetException | MailjetSocketTimeoutException e) {
			log.error("{}",e.getMessage());
			e.printStackTrace();
		}
		return Mono.empty();
	}

	private JSONObject buildContactJSON(MailSendRequest.Address from) {
		return new JSONObject().put("Email", from.getEmail()).put("Name", from.getName());
	}

	private JSONArray buildRecipientArray(Collection<MailSendRequest.Address> to) {
		return new JSONArray(to.stream()
		                       .map(this::buildContactJSON)
		                       .collect(Collectors.toList()));
	}
}

Logs :
toString of the MailjetRequest object :

2021-08-10 01:54:01.110 DEBUG 989950 --- [ntLoopGroup-3-6] i.m.m.core.services.MailServiceImpl      : mailjet request : {"Action":"","Filters":"{}","Action ID":0,"Resource":"send","Body":"{\"Messages\":[{\"HTMLPart\":\"<h3>Dear passenger 1, welcome to <a href='https://www.mailjet.com/'>Mailjet<\\/a>!<\\/h3><br />May the delivery force be with you!\",\"TextPart\":\"Bienvenue sur la plateforme Mouridprofessionals\",\"CustomID\":\"AppGettingStartedTest\",\"From\":{\"Email\":\"nureynisow@gmail.com\",\"Name\":\"Ousmane\"},\"To\":[{\"Email\":\"nureynisow@gmail.com\",\"Name\":\"Mertie\"}],\"Subject\":\"Bienvenue sur la plateforme Mouridprofessionals\"}]}"}
  • verbose mode of mailjet client:
=== HTTP Request ===
POST https://api.mailjet.com/v3.1/send
Accept-Charset:UTF-8
Accept:application/json
user-agent:mailjet-apiv3-java/v4.5.0
Content-Type:application/json
=== HTTP Response ===
Receive url: https://api.mailjet.com/v3.1/send
Status: 400
date:Mon, 09 Aug 2021 23:54:01 GMT
null:HTTP/1.1 400 Bad Request
content-length:177
x-mj-request-guid:29bc635f-ab2e-4570-8710-590377173a78
content-type:application/json; charset=UTF-8
Content:
null
2021-08-10 01:54:01.481 ERROR 989950 --- [ntLoopGroup-3-6] i.m.m.core.services.MailServiceImpl      : null
com.mailjet.client.errors.MailjetServerException
	at com.mailjet.client.MailjetResponseUtil.validateMailjetResponse(MailjetResponseUtil.java:40)
	at com.mailjet.client.MailjetClient.post(MailjetClient.java:283)

Hi @nureynisow,

thanks for raising this issue!
the code provided works for me. Also, the request body seems legit.

From the stacktrace provided, I can guess you might not be using the latest version of the package.
Could you please provide the version of the package, and, if that's not the 5.2.0, try to update the version?
If that's the 5.2.0, could you please provide the network dump of the request, using fiddler/charles/wireshark/etc?

Hi @goacoustic ,

it's work fine for the 5.2.0 version. However the developer guide on the website should be updated. Deprecated constructor are used for the mailjetClient.

Thanks for your help ;)