I am posting request using httpClient like:
I have imported HttpClientModule in app.module.js for http get and post request.
const httpOptionsWithCookie = { headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded'}), withCredentials: true };
this.baseUrl ("link of API")
postArguments is an object like " {name:"xyz",company:"abc"}"
this.http.post(this.baseUrl, postArguments, httpOptionsWithCookie);
I am using angular5 in front end and spring mvc at back end I am new in angular5.
API side code :
**spring mvc imports
@RestController
@RequestMapping(value = "/api/leavePeriod")
public class LeavePeriodControllerApi {
private static LogHelper logHelper = LogHelper
.getInstance(LeaveApplicationControllerApi.class);
@Autowired
HttpSession session;
@Autowired
Environment env;
@Autowired
ILeavePeriodService service;
@Autowired
ISecurityCommonService securityCommonService;
@Autowired
ILeaveCategoryService leaveCategoryService;
@Autowired
ICompanyService companyService;
@Autowired
LeavePeriodValidator leavePeriodValidator;
private User user = new User();
private String LOGINUSER = "";
@RequestMapping(value = "/viewLeavePeriod", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, Object> viewLeavePeriod( @ModelAttribute LeavePeriodForm form,HttpServletRequest request,@RequestParam(value ="companyId" ,required = false)
String companyId, @RequestParam(value ="leaveCategoryId", required = false)
String leaveCategoryId )throws HRAlignBaseException {
user = (User) session.getAttribute("user");
LOGINUSER = "LoginUser " + user.getEmpName() + " loginuser empCode "
+ user.getEmpCode();
Map<String, Object> returnMap = new HashMap<String, Object>();
try{
if(companyId!= null && companyId!="")
{
form.setCompanyId(Integer.parseInt(companyId));
}
if(leaveCategoryId!= null && leaveCategoryId!="")
{
form.setLeaveCategoryId(Integer.parseInt(leaveCategoryId));
}
returnMap = service.view(form);
returnMap.put("periodList", form.getLeavePeriodVoList());
returnMap.put("comId", form.getCompanyId());
returnMap.put("leaveCatId", form.getLeaveCategoryId());
} catch (Exception e) {
e.printStackTrace();
}
logHelper
.info("################"
+ LOGINUSER
+ "############# END Enter LeavePeriodController viewLeavePeriod");
return returnMap;
}
}
the same http post request work in angularjs but it's not working in angular5.
Also when doing the same http post from ARC, it's working fine but not working for angular5