Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

React native post-request with fetch is not working

chris080

New Coder
Hey I have a problem with my react native app.

I am very desperate, because I can't find a solution for more than a week.

I am sending data with react native and fetch to my express server. This is my fetch functionality:

JavaScript:
fetch('http://192.168.1.155:4000/login', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                userName: userName,
                password: password,
            }),
        })
            .then((response) => response.text)

My express server listens to this route:

app.post("/login", (req, res) => {
    console.log("Request: ", req.query)
    userRepository.login(req.query.userName, req.query.password)
        .then(() => {
            res.status(200).send("Login sucessfull")
        })
        .catch(err => {
            res.status(401).send("" + err) //TODO Return as string or how is it supposed to be?
        })
})

I tested to connect to the server with postman and I get response code 200 with the message "Login sucessfull"
But when I post it with my react native app, nothing happens. I logged the request and I get an empy object {} when I post with my app. When I post with postman, I get the login data I've sended
(And yes, I know, this isn't very secure to send login data. This is just for educational things. Encrypting and data security will be added after that)

What I thought would be, that I have to open the ports. I opened them, since my phone is another device than my laptop, which could explain, why postman can post and my app not. But it doesn't change anything. Actually I can connect with my smartphone to the server with a "get"-request over this route:

JavaScript:
app.get("/", (req, res) => {
    res.send("Mainpage for faster debugging")
})

So the connection is not the problem. And since postman (and my phone-browser) can work it out, the server is also okay. So I think the problem is in my react native fetch function. Or did I have to set up something? I use the app on android.
 
Last edited by a moderator:
Hi there,

Welcome to Code Forum!

Can you please post your code within the BBCode? It can be identified as the </> icon in the editor toolbar.
 

New Threads

Buy us a coffee!

Back
Top Bottom