0

I simply want to send a value to my rest api in a fetch method but can't seem to get it to work. This is what I have currently:

React:

getFloorplans() {
        fetch('/api/ssid')
            .then(response => response.json())
            .then(message => {
                this.setState({
                    floorplan: message[0]
                })
            });

        fetch('/api/ssid', {
            method: "POST",

            //make sure to serialize your JSON body
            value: {ssid: this.state.SSID}
        })
            .then( (response => response.json()
                .then(message => {
                    console.log(message);
                })
        ))};

Spring boot rest api:

@RestController
public class FloorplanController {
    //Floorplan floorplan = new Floorplan();

    @RequestMapping("/api/ssid")
    public void someMethod(@RequestParam String value) {
        System.out.println(value);
    }
}

This is the error I'm currently getting: enter image description here

4
  • 1
    You should use @PostMapping("/api/ssid") if you are performing a POST request, the HTTP method is not specified at the moment. Second thing, I don't know a lot of React, but if you are sending your value as a JSON body you should use @RequestBody in your method. Commented Oct 29, 2020 at 10:41
  • 1
    Also, I think you have to use body instead of value, as that is what fetch expects: developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch Commented Oct 29, 2020 at 10:43
  • And how do I then get the data from the body in my controller? Commented Oct 29, 2020 at 10:47
  • 1
    I'm not familiar with Spring but you should have some request parameter to where extract the body @Dylan75 Commented Oct 29, 2020 at 11:15

2 Answers 2

0

The exception is telling you that you are missing the request parameter. That's mean that value for @RequestParam String value in your controller is missing. The request should look like this http://yourhost/api/ssid?value=somevalue

Sign up to request clarification or add additional context in comments.

Comments

0

You should set headers, and params in body.

fetch('/api/ssid', {
     method: "POST",
     headers: {
          'Content-Type': 'application/json'
     },
     body: {ssid: this.state.SSID}
})

2 Comments

This only confirms what data you're sending but I think the problem lies in my controller but I don't know how to extract the body data there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.