Home > Software engineering >  Unable to read header value sent by React from Spring-Boot
Unable to read header value sent by React from Spring-Boot

Time:09-03

I would like to send the value to the spring boot through the header using the http put method in React.

So I made an api call code by using axios.

import axios from 'axios';
import {hashPwd} from '../../login/presentation/Encryptpwd'

export async function  ChangeNewPwd(newPwd) {
    alert(newPwd);
    const hash = hashPwd(sessionStorage.getItem("memberHash") newPwd);
    alert(hash);
    let result;
    return await axios.put("/member/pwd",{
        headers: {
            Authorization:hash
        }
    })
    .then(function(res){
        result = res.data;
        return result;        
    })
    .catch(function(error){
        console.log(error);
        result = 500;
        return result;
    })
}

But spring boot cannote read header value and sending this error

w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'Authorization' for method parameter type String is not present]

I have no idea why I'm getting this error. How can I solve this problem?

I'll show my spring boot code below.

    @PutMapping("/pwd")
    public int updatePwd(@RequestHeader("Authorization") String autho){
        logger.info("Changing pwd!!!");
        logger.info(autho);
        return 500;
    }

CodePudding user response:

I think the headers need to be passed in the third argument of the put method. The second argurment is for the data to be sent in the body.

await axios.put("/member/pwd", null, {
  headers: { Authorization: hash }
})
  • Related