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

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);
    
    点赞 评论

相关推荐 更多相似问题