I am creating application with front-end using Angular-4 and backend using spring boot(RestAPI),JPA,MySQL DataBase. I am successfully able to retrieve/get stored data from backend to frontend, but while posting the data from frontend to backend getting an error as : "Response for preflight has invalid HTTP status code 403".
code snippet for frontend Angular code :
myFoo(productData : Product) : Observable<any>{
let headers = new Headers();
headers.append( 'Content-Type', 'application/json' );
let myOptions = new RequestOptions({ headers: headers });
return this._http
.post("http://localhost:8081/..(URI)../productslist",productData,myOptions)
.map( (resp : Response) => resp.json() )
}
where productData passed from Controller class into above method argument is of Object type:
productData : any = {
price: 3500,
productavaliabledate: "March 13, 2018",
productcode: "GND-124",
productid: 6,
productimage: "https://i.imgur.com/sJUpSpt.jpg",
productname: "dummy1",
productrating: 3
}
code snippet for backend Spring RestAPI code is :
@RestController
public class OnlineShoppingController {
@Autowired
private ProductService productService;
@RequestMapping(path="/productslist",method =RequestMethod.POST)
public Product createProduct(@Valid @RequestBody Product product) {
return productService.createProduct(product);
}
}
where Product is dto/Entity object ie:
@Entity
@Table(name="product_table")
public class Product {
@Id
@Column(name="productid", columnDefinition="INT(100)")
private int productid;
@Column(name="productname")
private String productname;
@Column(name="productcode")
private String productcode;
@Column(name="productavaliabledate")
private String productavaliabledate;
@Column(name="price" , columnDefinition="INT(100)")
private int price;
@Column(name="productrating", columnDefinition="INT(50)")
private int productrating;
@Column(name="productimage")
private String productimage;
//getter,setter,constructor using field are also provided
}
csrflike thishttp.csrf().disable()in spring security or passcsrf-tokenwith request header fromangular