繁体   English   中英

我可以使用 body raw 发送数据,但不能使用 postman 中的 post 方法使用 formdata

[英]I can send data using body raw but not using formdata using post method in postman

我只是想用我的更新配置文件 api 上传配置文件图片。 我可以使用 json (post) 方法简单地通过 body raw 发送数据,但是对于上传文件,我使用的是 formdata 并且我无法发送任何数据。 我只收到请提供输入详细信息的回复。

这是我的 api 控制器代码

 public function update_profile()
{
    $this->default_file();
    $responseData = array();
    if(!empty($_POST['u_id']))
    {
        $id = $_POST['u_id'];
        $userData['u_id'] = $id;
        $userData['username'] = $_POST['username'];
        $userData['usermob'] = $_POST['usermob'];
        $userData['userlocation'] = $_POST['userlocation'];

        $update_profile = $this->apm->update_profile($userData);

        if(!empty($update_profile))
        {
            $id = $_POST['u_id'];
            $userDetails = array();
            $userDetails['id'] = $id;
            $getUserDetails = $this->apm->getUserDetails($userDetails);

            $responseData['u_id'] = $getUserDetails['result']['u_id'];
            $responseData['username'] = $getUserDetails['result']['username'];
            $responseData['useremail'] = $getUserDetails['result']['useremail'];
            $responseData['usermob'] = $getUserDetails['result']['usermob'];
            $responseData['userlocation'] = $getUserDetails['result']['userlocation'];

            $responseArray = array(
                'apiName' => 'update profile',
                'version' => '1.0.0',
                'responseCode' => 200,
                'responseMessage' => "Your profile updated successfully",
                'responseData' => $responseData
            );
        }
        else
        {
            $responseArray = array(
                'apiName' => 'update profile',
                'version' => '1.0.0',
                'responseCode' => 204,
                'responseMessage' => "error in updating profile",
                'responseData' => null//$responseData
            );
        }
    }
    else
    {
        $responseArray = array(
            'apiName' => 'update profile',
            'version' => '1.0.0',
            'responseCode' => 204,
            'responseMessage' => "Sorry, please provide your input details.",
            'responseData' => null//$responseData
        );
    }
    echo json_encode($responseArray);
    die();
}

我正在提交代码而不添加图像部分代码来检查使用formdata的简单提交数据。

这是我的 api 模式代码

public function update_profile($userData)
{
    return $this->db->update('users', $userData, array('u_id' => $userData['u_id']));
}

public function getUserDetails($userDetails = array())
{
    $arrData = array();
    if($userDetails['id'])
    {
        $where = "u_id='". $userDetails['id']."'";
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where($where);
        $result = $this->db->get()->result_array();
        if(!empty($result))
        {
            $arrData['result'] = $result[0];
        }
        else
        {
            $arrData['result'] = '';
        }
    }
    return $arrData;
}

这是默认文件代码

 function default_file(){
    header("Access-Control-Allow-Origin: * ");
    header("Access-Control-Allow-Headers: Origin,Content-Type ");
    header("Content-Type:application/json ");
    $rest_json = file_get_contents("php://input");
    $_POST = json_decode($rest_json,true);
}

请帮助我使用 formdata 运行我的代码,以便我可以通过该 api 上传图像。

FormData 对象未编码为 JSON(这是明智的,因为 JSON 不支持 File 数据类型,而 FormData 的最大好处是它支持)。

FormData 对象将被编码为multipart/form-data ,它将被 PHP 自动解析并用于填充$_POST$_FILES

不幸的是,您的代码( $_POST = json_decode($rest_json,true); )用结果覆盖了您想要的数据,以尝试解析为 JSON,而不是 JSON。

注意:使用 FormData 时,请确保不要覆盖请求中的Content-Type标头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM