首页 > 解决方案 > 在 python 和 raspberry 中看不到随 post 请求发送的 json 内容

问题描述

我尝试将 json 从树莓派 Pi4 发送到 Laravel 项目,但是当我尝试显示 Json 的内容时,我什么也看不到,只有一个空方括号 []

Python 代码形式为 raspberry :此函数从文件中读取并将其转换为 json 文件并将其发送到 Laravel:

def readFile():
    with open("/home/pi/Desktop/Progetti SIoTD/device.txt", "r") as file:
        for i in file:
            line, *lines = i.split()
            if line in mac_dict:
                mac_dict[line] += lines
            else:
                mac_dict[line] = lines
    print(mac_dict)
    print("\n")

    json_obj = json.dumps(mac_dict, indent=4)
    with open("/home/pi/Desktop/Progetti SIoTD/Bluetooth/mac_addr.json", "w") as json_file:
        json_file.write(json_obj)

    print(json_obj)

    ip = 'http://192.168.1.6:14000'
    headers = {'Content-Type': 'application/json'}
    req = requests.post(ip, headers=headers, data=json_obj)

Laravel 控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DictionaryController extends Controller
{
    public function index()
    {
        //
    }

    public function store(Request $request)
    {
        return response()->json($request->all());
    }

    public function show($id)
    {
        //
    }

    public function update(Request $request, $id)
    {
        //
    }

    public function destroy($id)
    {
        //
    }
}

Laravel 路由接口

Route::get('dictionary', [DictionaryController::class, 'store'])->name('dict');

这是我的输出

我怎样才能解决这个问题?

标签: pythonjsonlaravel

解决方案


推荐阅读