1

I have a deployed spring server on my EC2 instance which is throwing this error. For context, from my index.html, I have a form with a submit button, which sends a post call. This post call is in my resource. The POST call works perfectly fine on my localhost, but when I deploy it on EC2, it throws an error in the title. This is also all built on spring boot.

HTML

<script type="text/javascript" src="js/Email.js"></script>
(other divs with headers and titles in between)
<form onsubmit="sendEmail(this)">
(input tags over here and a button)
</form>

Email.js

function sendEmail(form) {
    var bodyVal = "{\"email\" : \"" + "[email protected]" + "\",\"message\" : \"" + "hello this is message" + "\"}"
            const req = new XMLHttpRequest();
            var myHeaders = new Headers();
            myHeaders.append('Content-Type', 'application/json');
            fetch("http://(domain name)/sendmail", {
                method: 'POST',
                mode: 'cors',
                cache: 'no-cache',
                headers : myHeaders,
                body: bodyVal
            }).then(function(response) {

            });
    return false;
}

Resource which targets the /sendmail endpoint

@CrossOrigin
    @PostMapping(path = "/sendmail", consumes = "application/json", produces = "application/json")
    public void sendMail(@RequestBody Map<String, Object> payload) throws Exception {
        String message = payload.get("message").toString();
        String fromEmail = payload.get("email").toString();
        String body = "";
        Unirest.post("https://api.sendgrid.com/v3/mail/send")
                .header("authorization", SEND_GRID_AUTH)
                .header("content-type", "application/json")
                .body(body)
                .asString();

    }

I've seen some solutions of putting some extra tags in the html to solve some security issues, but I'm not sure what's the proper solution for this. If someone could help me out it would be great.

6
  • post log trace here? Commented May 15, 2021 at 19:19
  • Probably one of the answers will help - stackoverflow.com/questions/54005723/… P.S. I think that you are using @Controller instead of @RestController Commented May 16, 2021 at 2:31
  • @VadymVL I actually am using RestController at the top of my class holding my api calls Commented May 16, 2021 at 17:28
  • @priyranjan the only output in my log is what is in the header. It is all that shows. Commented May 16, 2021 at 17:29
  • what is the value of SEND_GRID_AUTH? it should be "basic <your base64 value>. can you please check this? Commented May 16, 2021 at 18:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.