-4

I am learning how to receive the inputs then call the backend service that has parameters (numberX, numberY, numberZ). This backend service calculates the entered inputs from client (there is no database involved just the service). I want to use the fetch API, should I use the get or post? The API is 'http://localhost:1234/?numberX=25&numberY=12&numberZ=21'.

export const Calculator = () => {
  const [numberX, setNumberX] = useState('0.0');
  const numberXAsNumber = Number(numberX);
  const [numberY, setNumberX] = useState('0.0');
  const numberYAsNumber = Number(numberY);
  const [numberX, setNumberZ] = useState('0.0');
  const numberZAsNumber = Number(numberZ);
return ( 
  // Calculator
  <Form>
    <Input type="number" name="numberX" label="Number X" 
           value={numberX} onChange=(e => setNumberX(e.target.value)} 
           min="0.0" required />
    <Input type="number" name="numberY" label="Number Y" 
           value={numberY} onChange=(e => setNumberY(e.target.value)} 
           min="0.0" required />
    <Input type="number" name="numberZ" label="Number Z" 
           value={numberZ} onChange=(e => setNumberZ(e.target.value)} 
           min="0" required />
    <Button type=submit>Calculate</Button>
  </Form>
  <div>
            { Number X > -1 && 
                <p>Number X: {numberX} X</p>
            }

            {
                number Y > -1 &&
                <p>Number Y: {numberY} Y</p>
            }

            {
                numberZ > -1 &&
                <p>Number Z: {numberZ} Z</p>
            }
  // Below is the display of calculated inputs - My goal is to have another component    like this.
  <DisplayCalculatedInputs />
  </div>
   
)
}
2
  • 4
    Your only concrete question here seems to be whether you should use the GET or POST method - we can't tell you this, it's entirely down to the API you're trying to call. Commented Oct 31 at 9:41
  • 4
    If the API responds to GET, then use GET. If the API responds to POST, then use POST. If you're not sure what it responds to - check the documentation, or the code (if available), or make a GET/POST request to it and check which one succeeds or doesn't. Given that you're hosting it locally, only ever you can determine how it works, Commented Oct 31 at 9:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.