Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

POST request not working when processed from frontend

Avatar

Level 2

Hello there, I'm facing a strange issue when I try to make a POST request from my frontend(I'm currently using React, but i'm doing these requests from localhost:4502). I know for sure that the servlet it's working because when I do a POST request from Postman works correctly. When I do it from the frontend it gives me 403(Forbidden). Why it has different behaviour when I do the legit same things?
Info about servlet: 
Servlet temporary binded to /bin/vendorname/utils

 

8 Replies

Avatar

Community Advisor

You should check CORS configuration, check the allowed method types,

Allowed Methods

  • "supportedmethods"
  • List of method parameters indicating which HTTP methods can be used when making the actual request.

Understand Cross-Origin Resource Sharing (CORS) with AEM | Adobe Experience Manager

Avatar

Level 2

Thank you for your answer @Nitin_laad 
Currently my cors settings are:
allowedorigins: "http://localhost:3000"
allowedmethods(supportedmethods): ["GET", "POST", "HEAD"]

supportedheaders: [ "Authorization""Origin", "Accept", "X-Requested-With", "Content-Type", "Access-Control-Request-Method""Access-Control-Request-Headers"]

Avatar

Community Advisor

@lezzowski1 You may have to remove the POST value from the filter in  "Apache Sling Referrer Filter" and "Adobe Granite CSRF Filter" configuration - http://localhost:4502/system/console/configMgr

Avatar

Community Advisor

hi,

You need to pass the authentication(credentials with post request) since it is Author.

Try in publish instance, it should work.



Arun Patidar

Avatar

Level 2

Good news, my servlet is currently working while binded on path but if I try to bind it on a component with SlingServletResourceTypes my request status code is 200 but it does nothing. It just says Content Modified or something similar instead of my json. Any thoughts?

Avatar

Community Advisor

Can you share, how did you registered the servlet.

Better to use selector to avoid resolution of default post servlet.



Arun Patidar

Avatar

Level 2

Seems like the problem was this one: I wasn't setting "methods = "POST" " in the decorator. I'm kinda shocked because why should the servlet work as intended when binded to path (and I didn't explicitly set "methods = POST") but not when binded to component? It's totally crazy