我将此作为测试发送到测试网络服务器,但是响应虽然它是201,这意味着它得到了它,它没有显示我想发送的发布数据:
<?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);
?>
我提出的请求格式是否不正确?
我可以看到它没有得到帖子数据,因为当我做一个响应体的dsm时,它就不存在了。
答案 0 :(得分:2)
这对我有用,看起来我需要添加标题:
$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);