dongshuiga2826 2017-04-28 23:36
浏览 90
已采纳

Guzzle不发送POST参数

I am sending this as a test to a test webserver, but the response although its a 201 which means it got it, it does not show the posted data I want to send:

<?php
  $url = "https://jsonplaceholder.typicode.com/posts";
  $client = \Drupal::httpClient();

  $post_data = array('color' => 'red');
  $response = $client->request('POST', $url, [
    'form_params' => $post_data,
    'verify' => false
    ]);
  $body = $response->getBody();
  dsm($body);
?>

Is the format of the request I made incorrect?

I can see that it is not getting the post data because when I do a dsm of the response body, it isn't there.

  • 写回答

1条回答 默认 最新

  • duandou8120 2017-05-02 19:03
    关注

    This worked for me, looks like I needed to add the headers:

    $url="https://jsonplaceholder.typicode.com/posts";
        $client = \Drupal::httpClient();
        $post_data = $form_state->cleanValues()->getValues();
        $response = $client->request('POST', $url, [
        'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
        'form_params' => $post_data,
        'verify'=>false,
      ]);
        $body = $response->getBody()->getContents();
        $status = $response->getStatusCode();
        dsm($body);
        dsm($status);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况