You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Expo应用无法与Flask服务器建立连接的问题求助

Hey there, let's break down what's going wrong here and fix that network error step by step!

1. Missing Protocol & Wrong Localhost Address

Your fetch URL 127.0.0.1:5000/post has two issues:

  • It's missing the http:// protocol header, which is required for fetch to recognize it as a valid network address.
  • In Expo/React Native simulators, 127.0.0.1 points to the simulator itself, not your computer running the Flask server.

Fix this by:

  • Using your computer's local network IP (like http://192.168.1.100:5000/post) if you're testing with Expo Go on a physical phone.
  • Using http://localhost:5000/post or your computer's IP if testing on a simulator.

2. Incorrect POST Request Format

You're passing a raw object as the body, but fetch won't automatically convert it to JSON, and you haven't set the correct request headers. Update your frontend code like this:

fetch("http://你的电脑局域网IP:5000/post", {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ 'base64': photoData["base64"] }),
})
.then(response => response.json())
.then(result => {
  console.log("******************\nRESULT");
  console.log(result);
})
.catch(err => {
  console.log(err);
})

3. Backend is Reading from URL Params, Not POST Body

Your Flask code uses request.args['base64'], which pulls data from URL query parameters (like /post?base64=xxx), but your frontend sends data in the POST JSON body. Update your backend to read the JSON payload instead:

from flask import request, jsonify
import base64

@app.route('/post', methods=['POST']) # Use uppercase POST for consistency
def webhook():
    data = request.get_json()
    file = data['base64']
    rv = base64.b64encode(str.encode(file)) # Convert to bytes
    rv = rv.decode('ascii') # Convert back to string
    return jsonify(rv) # Return properly formatted JSON response

Extra Checks to Ensure Connection

  • Run your Flask server with app.run(host='0.0.0.0', port=5000) so it listens on all network interfaces (not just 127.0.0.1).
  • Make sure your computer's firewall isn't blocking incoming traffic on port 5000.
  • If using Expo Go on a phone, confirm it's connected to the same WiFi network as your computer.

内容的提问来源于stack exchange,提问作者Екатерина Яцкив

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 08:37:36