【问题标题】:How do i send a PUT request in Laravel我如何在 Laravel 中发送 PUT 请求
【发布时间】:2021-09-05 22:04:21
【问题描述】:

我正在尝试通过向 ServiceDesk plus api 发送 http put 请求来更新数据。在使用系统自带的控制台时,它运行良好,但是当我尝试从 Laravel 向同一个 api 发送请求时,它就不起作用了。

来自下方控制台的请求

我正在尝试使用下面的代码向相同的 url 发送请求。

 private function openTicket($notification)
 {
    $data = json_encode(['input_data' => ['request' => ['subject' => $notification->subject,
            'description' => $notification->description,
            'status' => ['name' => 'Open']]]]);


    $request_id = $notification->request_id;
    $response = Http::withHeaders([
            'technician_key' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX',
            'Accept' => 'application/json'
    ])->put('http://localhost:8082/api/v3/requests/' . $request_id, $data);

     dd($response);
    }

我收到一个错误400 bad request

【问题讨论】:

  • dd($response->body()); 输出什么?
  • 也许您需要发送任何标题或类似内容
  • 让我试试@shaedrich,等等
  • @shaedrich 这是输出"{"response_status":{"status_code":4000,"messages":[{"status_code":4001,"field":"input_data","type":"failed","message":"Value not provided"}],"status":"failed"}}"
  • 好像您没有提供所需的值。你有什么想法吗?

标签: php json laravel guzzle put


【解决方案1】:

我设法找到了解决方案,如下所示,

private function openTicket($notification): bool
    {
        $data = json_encode(['request' => ['subject' => $notification->subject,
            'description' => $notification->description,
            'status' => ['name' => 'Open']]]);

        $request_id = $notification->request_id;
        $response = Http::withHeaders([
            'technician_key' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
            'Content-Type' => 'application/x-www-form-urlencoded'
        //added asForm() before put
        ])->asForm()->put('http://localhost:8082/api/v3/requests/' . $request_id, [
            'input_data' => $data
        ]);
      
        if ($response->status() == 200) {
            return true;
        }
        return false;
    }

我在 put 函数之前添加了 asForm()。这是因为 asForm() 表示请求包含表单参数。我还从

修改了 $data 对象
$data = json_encode(['input_data' => ['request' => ['subject' => $notification->subject,
            'description' => $notification->description,
            'status' => ['name' => 'Open']]]]);

$data = json_encode(['request' => ['subject' => $notification->subject,
            'description' => $notification->description,
            'status' => ['name' => 'Open']]]);

然后它按我的预期工作。

【讨论】:

    【解决方案2】:

    你不应该做json_encode,laravel Http 模块会自动为你做。我认为您的数据现在是 json_encoded 两次。

    $data = [
      'input_data' => [
        'request' => [
          'subject' => $notification->subject,
          'description' => $notification->description,
          'status' => ['name' => 'Open']
        ]
      ]
    ]);
    
    
        $request_id = $notification->request_id;
        $response = Http::withHeaders([
                'technician_key' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX',
                'Accept' => 'application/json'
        ])->put('http://localhost:8082/api/v3/requests/' . $request_id, $data);
    
         dd($response);
    

    我刚刚注意到。根据您在屏幕截图中提供的文档,数组中的 input_data 嵌套级别不应该存在

    $data = [
      'request' => [
        'subject' => $notification->subject,
        'description' => $notification->description,
        'status' => ['name' => 'Open']
      ]
    ]);
    

    【讨论】:

    • 它不工作,尝试按照它仍然给出同样的错误
    • 我刚刚注意到文档,input_data 嵌套级别没用。我更新了我的答案
    • 我设法解决了,谢谢@gbalduzzi
    猜你喜欢
    • 2012-07-09
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 2011-06-13
    • 2019-12-12
    相关资源
    最近更新 更多