I have a POST and a GET method served from the same domain from where the request is getting fired. Though, I felt that CORS wont be an issue, but it seems to be, as the port is different.
After adding the headers, I could manage to serve my GET request, but POST is not working.
I have added the headers on the server side, below is the code:
@SuppressWarnings("unchecked")
@RequestMapping(method=RequestMethod.OPTIONS,value="/refer")
public ResponseEntity<?> options(){
return new ResponseEntity(getHeaders(), HttpStatus.OK);
}
@RequestMapping(method=RequestMethod.GET,value="/search",produces="application/json")
public ResponseEntity<?> searchCook(@ModelAttribute("cookSearch") CookSearch cookSearch){
ResponseDto<List<DtoCook>> response = new ResponseDto<>();
List<DtoCook> results = serviceCook.search(cookSearch);
response.setData(results);
response.setMessage(results.size()+" found.");
return new ResponseEntity<>(response, getHeaders(),HttpStatus.OK);
}
@SuppressWarnings("unchecked")
@RequestMapping(method=RequestMethod.POST,value="/refer",produces="application/json",consumes="application/json")
public ResponseEntity<?> referCookPost(@ModelAttribute("dtoCookRefer") DtoCookRefer dtoCookRefer,BindingResult result) {
System.out.println("in cook rest controller");
// set the headers to allow CORS
MultiValueMap<String, String> headers = getHeaders();
System.out.println(dtoCookRefer.getCookName()+ " phone");
ResponseDto<DtoCookRefer> respDto = new ResponseDto<>();
respDto.setData(dtoCookRefer);
//run Spring validator manually
new CookReferValidator().validate(dtoCookRefer, result);
if(serviceCookRefer.checkUniquePhNum(dtoCookRefer)) {
respDto.setMessage("Phone number already present");
return new ResponseEntity<>(respDto, headers,HttpStatus.UNPROCESSABLE_ENTITY);
}
// validate cook data
if(result.hasErrors()) {
//TODO set message source
respDto.setMessage("Improper data");
return new ResponseEntity<>(respDto, headers,HttpStatus.UNPROCESSABLE_ENTITY);
}else {
// save data to database
serviceCookRefer.referCook(dtoCookRefer);
// return proper response
respDto.setMessage("Processed Successfully");
return new ResponseEntity(respDto, headers, HttpStatus.OK);
}
}
private MultiValueMap<String, String> getHeaders(){
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String,String>();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Access-Control-Allow-Origin","http://example.org");
headers.add("Access-Control-Allow-Methods","GET,PUT,POST,DELETE,OPTIONS");
headers.add("Access-Control-Allow-Headers",
"Cache-Control,Pragma,Expires,Access-Control-Allow-Origin,"
+ "Access-Control-Allow-Methods,Content-Type,Transfer-Encoding,Date");
return headers;
}
Below is the code in Angular:
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import { CookRefer } from './cook-refer';
@Injectable({
providedIn: 'root'
})
export class CookReferService {
private _url : string = "http://123.121.166.243:8080/ffc/ext/cook/refer";
constructor(private _http : HttpClient) { }
headers = new HttpHeaders({
'Content-Type':'application/json',
'Access-Control-Request-Method':'POST'
});
options={headers:this.headers};
refer(cook : CookRefer){
return this._http.post<any>(this._url,cook,this.options);
}
}
- It looks like browser blocks CORS request. The request does not even reach the server.
- especially for POST it triggers a preflight request even before I added the headers in client (angular) side, but not for GET.
- why GET is working but not POST?