无法设置 Guzzle 内容类型
Can't set Guzzle Content Type
我正在尝试通过这种方式请求:
$body = [];
$body['holder_name'] = $full_name;
$body['bank_code'] = $bank_number;
$body['routing_number'] = $branch_number;
$body['account_number'] = $account_number;
$body['type'] = 'checking';
$client = new GuzzleHttp\Client([
'base_url' => [$url, []],
'headers' => ['content-type' => 'application/json', 'Accept' => 'application/json'],
'defaults' => [
'auth' => [$publishable_key, ''],
],
'body' => json_encode($body),
]);
问题是此请求的设置没有 Content-Type。
我做错了什么?
好的..问题是我在默认设置之外设置 body 和 headers。解决方案是:
$client = new GuzzleHttp\Client([
'base_url' => [$url, []],
'defaults' => [
'auth' => [$publishable_key, ''],
'headers' => ['content-type' => 'application/json', 'Accept' => 'application/json'],
'body' => json_encode($body),
],
]);
狂风 6
Guzzle will set the Content-Type header to
application/x-www-form-urlencoded
when no Content-Type header is
already present.
您有 2 个选择。
选项 1:直接在客户端上
$client = new GuzzleHttp\Client(
['headers' => [
'Content-Type' => 'application/json'
]
]
);
选项 2:基于每个请求
// Set various headers on a request
$client = new GuzzleHttp\Client();
$client->request('GET', '/whatever', [
'headers' => [
'Content-Type' => 'application/json'
]
]);
我遇到了与 Hubspot API 相同的问题,它需要将 application/json
设置为 POST 请求的内容类型。
我是这样改的
$client = new Client([
'base_uri' => 'https://api.hubapi.com/',
'timeout' => 5,
'headers' => ['Content-Type' => 'application/json']
]);
然后以常规方式执行我的请求
try
{
$response = $client->request('POST', '/contacts/v1/contact/email/test@test.com/profile',
['query' => MY_HUBSPOT_API_KEY, 'body' => $body]);
}
catch (RequestException $e) { print_r($e); }
希望对您有所帮助。
我正在尝试通过这种方式请求:
$body = [];
$body['holder_name'] = $full_name;
$body['bank_code'] = $bank_number;
$body['routing_number'] = $branch_number;
$body['account_number'] = $account_number;
$body['type'] = 'checking';
$client = new GuzzleHttp\Client([
'base_url' => [$url, []],
'headers' => ['content-type' => 'application/json', 'Accept' => 'application/json'],
'defaults' => [
'auth' => [$publishable_key, ''],
],
'body' => json_encode($body),
]);
问题是此请求的设置没有 Content-Type。 我做错了什么?
好的..问题是我在默认设置之外设置 body 和 headers。解决方案是:
$client = new GuzzleHttp\Client([
'base_url' => [$url, []],
'defaults' => [
'auth' => [$publishable_key, ''],
'headers' => ['content-type' => 'application/json', 'Accept' => 'application/json'],
'body' => json_encode($body),
],
]);
狂风 6
Guzzle will set the Content-Type header to
application/x-www-form-urlencoded
when no Content-Type header is already present.
您有 2 个选择。
选项 1:直接在客户端上
$client = new GuzzleHttp\Client(
['headers' => [
'Content-Type' => 'application/json'
]
]
);
选项 2:基于每个请求
// Set various headers on a request
$client = new GuzzleHttp\Client();
$client->request('GET', '/whatever', [
'headers' => [
'Content-Type' => 'application/json'
]
]);
我遇到了与 Hubspot API 相同的问题,它需要将 application/json
设置为 POST 请求的内容类型。
我是这样改的
$client = new Client([
'base_uri' => 'https://api.hubapi.com/',
'timeout' => 5,
'headers' => ['Content-Type' => 'application/json']
]);
然后以常规方式执行我的请求
try
{
$response = $client->request('POST', '/contacts/v1/contact/email/test@test.com/profile',
['query' => MY_HUBSPOT_API_KEY, 'body' => $body]);
}
catch (RequestException $e) { print_r($e); }
希望对您有所帮助。