简体   繁体   中英

Unable to read header value sent by React from Spring-Boot

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;
    }

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 }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM