mani786's avatar

Url request is working for GET but same URL not working for POST

I have a url request for GET and POST data but that url request is working for GET only please guide regarding this

http://localhost:8081/users

~~ import React, {useState} from 'react'; import axios from 'axios';

function Login() { var [Name, setName] = useState(''); var [Email, setEmail] = useState(''); var [Phone, setPhone] = useState('');

function LoginHandle(e){
    e.preventDefault();
    Name = e.target.name.value;
    Email = e.target.email.value;
    Phone = e.target.phone.value;
    console.warn(Name, ' ', Email, ' ',Phone);

    axios.post("http://localhost:8081/users",{
        Name,
        Email,
        Phone
    })
    .then(response => {
        console.log(response)
    })
    .catch(error => {
        console.warn(error)
    });
}

~~

0 likes
11 replies
LaryAI's avatar
Level 58

The issue might be with the server-side code that is not handling the POST request properly. To debug the issue, you can try the following steps:

  1. Check if the server is running and listening to the correct port (8081 in this case).
  2. Check if the server-side code is properly handling the POST request. You can use a tool like Postman to send a POST request to the same URL and see if it works.
  3. Check if there are any errors in the browser console or server logs.

If the issue is with the server-side code, you need to fix it to properly handle the POST request. If you don't have access to the server-side code, you can try contacting the server administrator or the API provider for help.

Here's an example of how to handle the POST request in Node.js using Express:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());

app.post('/users', (req, res) => {
  const { Name, Email, Phone } = req.body;
  // handle the POST request here
  res.send('OK');
});

app.listen(8081, () => {
  console.log('Server is running on port 8081');
});

In this example, we're using the body-parser middleware to parse the request body as JSON. Then, we're defining a route for the POST request to /users and handling the request by extracting the data from the request body and sending a response.

mani786's avatar

@LaryAI ~~

const express = require("express"); const mysql = require("mysql"); const cors = require("cors");

const app = express(); app.use(cors());

const db = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "pr1", })

app.get('/', (re,res)=> { return res.json("Welcome to Backend Side"); })

app.get('/users', (req, res)=> { const sql = "SELECT * FROM users"; db.query(sql, (err, data) => { console.warn(data); if(err) return res.json(err); return res.json(data); }) });

app.listen(8081, ()=>{ console.warn("Hello all People Listening") })

~~

Snapey's avatar

never hard code url in your code. You will not be able to deploy without editing your code

For your problem, show your routes file

mani786's avatar

@Snapey working on local and this is the server setting on local ~~ const express = require("express"); const mysql = require("mysql"); const cors = require("cors");

const app = express(); app.use(cors());

const db = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "pr1", })

app.get('/', (re,res)=> { return res.json("Welcome to Backend Side"); })

app.get('/users', (req, res)=> { const sql = "SELECT * FROM users"; db.query(sql, (err, data) => { console.warn(data); if(err) return res.json(err); return res.json(data); }) });

app.listen(8081, ()=>{ console.warn("Hello all People Listening") })

~~

Snapey's avatar

@mani786 please format you code blocks when posting

When I ask for your routes file, I'm talking about LARAVEL

mani786's avatar

@Snapey I understand but I did not build laravel project and directly using mysql through node

mani786's avatar

@Snapey The same URL . I can fetch all the data in my react JS app but when I POSt data on that URL so there is internal server error 500

Snapey's avatar

@mani786 So why ask a react question with a Node backend on a Laravel forum?

mani786's avatar

@Snapey I thought there would be someone having same issue before and there would also be someone having experience other than laravel

Please or to participate in this conversation.