耗时版本6的方法无法正常工作 [英] guzzle ver 6 post method is not woking

查看:48
本文介绍了耗时版本6的方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在邮递员中工作(原始格式的数据,具有application/json类型) 嘴巴6

is working in postman (raw format data with with application/json type) with guzzle6

url-http://vm.xxxxx.com/v1/hirejob/
{
        "company_name":" company_name",
        "last_date_apply":"06/12/2015",
        "rid":"89498"
}

因此正在创建响应201
却不知所措

so am getting response 201 created
but in guzzle

    $client = new Client();
    $data = array();
    $data['company_name'] = "company_name";
    $data['last_date_apply'] = "06/12/2015";
    $data['rid'] = "89498";
    $url='http://vm.xxxxx.com/v1/hirejob/';
    $data=json_encode($data);
    try {
            $request = $client->post($url,array(
                    'content-type' => 'application/json'
            ),array());

        } catch (ServerException $e) {
          //getting GuzzleHttp\Exception\ServerException Server error: 500
        }

我在vendor/guzzlehttp/guzzle/src/Middleware.php

第69行

 ? new ServerException("Server error: $code", $request, $response)

推荐答案

您实际上并没有设置请求正文,但是可以说,最简单的传输JSON数据的方法是使用专用的请求选项:

You're not actually setting the request body, but arguably the easiest way to transfer JSON data is by using the dedicated request option:

$request = $client->post($url, [
    'json' => [
        'company_name' => 'company_name',
        'last_date_apply' => '06/12/2015',
        'rid' => '89498',
    ],
]);

这篇关于耗时版本6的方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆