Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help to solve 400 bad request

Im trying to fetch an give data to a POST-method getNumbers in the server, that has a parameter id, which simply returns the id for debugging purposes. So in the Client i do as following:

 documentClickHandler=(number)=> {
    const requestOptions = {
      method: 'POST',
      headers: { 
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ id: number })
    };
    fetch("http://localhost:5000/api/Documents/GetNumber", requestOptions)
        .then(response => response.json())
        .then(result => console.log(result))
        .catch(error => { console.log('request failed', error); });
  }

Which only has the purpuse to give the id to the getNumber method in the server:


[HttpPost("GetNumber")]
public string GetNumber([FromBody] string id)
       return id;
} 

This gives me an error (400 bad request) and I dont know how to solve it. Any ideas? Im in desperate need of help.
like image 487
name Avatar asked Dec 04 '25 13:12

name


1 Answers

Remove word "number" from your code. It is a reserved word

try this code:

 documentClickHandler=(documentId)=> {

   var id="123"; // try this value at first, after this use your value

    const requestOptions = {
      method: 'POST',
  
      body: {id: id}
    };
    fetch("http://localhost:5000/api/Documents/GetNumber", requestOptions)
        .then(response => response.json())
        .then(result => console.log(result))
        .catch(error => { console.log('request failed', error); });
  }

and action

[Route("~/api/Documents/GetNumber")]
public string GetNumber( string id)
       return id;
} 
like image 200
Serge Avatar answered Dec 07 '25 02:12

Serge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!