0

I want to send my JWT token in a post request to my spring boot application.

 $.ajax({
    type: "POST",
    contentType: "application/json",
    url: cartUrl,
    data: JSON.stringify(purchase),
    dataType: 'json',
    header: {
        'Authorization': token
    },
    success: function (data) {
        console.log(JSON.stringify(data));
        if (data['status'] == "success") {
            var content = data.successResponse;
            $('#quick-cart').modal('toggle');
            //$(".mini-cart").toggleClass('active');
            cartPopulate(content.purchases)
        } else if (data['status'] == 'error') {

        }
    },
    error: function (e) {

    }
});

And below is my spring boot controller method

 @PostMapping(value = {"/business/{businessId}/cart", 
  "/business/{businessId}/cart/{cartId}"},
        produces = MediaType.APPLICATION_JSON_VALUE,
        consumes = MediaType.APPLICATION_JSON_VALUE)
 @ResponseBody
 protected Object addItemToCart(
                               @PathVariable String businessId,
                               @RequestHeader(name = "Authorization", required = true) String token,
                               @RequestBody Purchase purchase,
                               @PathVariable(required = false) String cartId,
                               HttpServletRequest request) {

    ResponseEntity<ShoppingCart> updatedCart = cartService.addItemToCart(businessId, token, purchase);
    ResponseContainer response = new ResponseContainer("success", updatedCart.getBody());

    return response;
}

I am getting this error " Resolved [org.springframework.web.bind.MissingRequestHeaderException: Missing request header 'Authorization' for method parameter of type String]". Any suggestions,please?

user3692033
  • 585
  • 3
  • 8
  • 21
  • Does this answer your question? [How to send a token with an AJAX request from jQuery](https://stackoverflow.com/questions/35861012/how-to-send-a-token-with-an-ajax-request-from-jquery) – Tommy Brettschneider May 23 '20 at 06:47
  • check in the browser developer tools - does the header get sent? probably not, since it's `headers:` not `header:` – Jaromanda X May 23 '20 at 06:48

0 Answers0