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

使用Postman访问Flask的/cadastrar_noticia路由时出现404错误求助

Flask POST路由返回404 Not Found问题排查

我正在使用Flask开发Web应用,尝试通过Postman访问用于在数据库中注册新文章的特定路由时遇到问题。当向/cadastrar_noticia路由发送POST请求时,我收到了"404 Not Found"错误。

相关代码

@user_bp.route('/cadastrar_noticia', methods=['POST'])
def cadastrarNoticia():
    if not session.get('logged_in'):
        return jsonify("Usuário nao está logado no sistema")
    
    data = request.get_json()

    # Verifica se todos os campos obrigatórios estão presentes nos dados da solicitação
    required_fields = ['titulo', 'resumo', 'noticia', 'site_id', 'categoria_id']
    for field in required_fields:
        if field not in data or not data[field]:
            return jsonify({'error': f'Campo {field} ausente ou vazio!'}), 400
        
    try:
        conn = connect_db()
        cursor = conn.cursor()

        # Insere a nova notícia no banco de dados
        cursor.execute("INSERT INTO Sites_noticias (titulo, resumo, noticia, site_id, categoria_id) VALUES (%s, %s, %s, %s, %s) RETURNING *",
                       (data['titulo'], data['resumo'], data['noticia'], data['site_id'], data['categoria_id']))
        
        new_noticia = cursor.fetchone()
        conn.commit()

    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        if 'conn' in locals():
            conn.close()

    return jsonify({'success': True, 'message': 'Notícia cadastrada com sucesso!', 'noticia': new_noticia}), 201

已完成的检查

  • 确认Flask代码中的路由定义无误
  • 验证Flask应用正在运行,服务器可通过5000端口访问
  • 尝试通过Postman访问应用中的其他路由均正常,仅该特定路由出现问题

请求帮忙理解错误原因并提供修复方案。

内容的提问来源于stack exchange,提问作者KauaKing

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 22:07:42