Проблема Ларавела с параметром в GuzzleHttp - PullRequest
0 голосов
/ 06 февраля 2019

Привет

Когда я использую GuzzleHttp\Client работает нормально, но form_params не работает, но он работал со мной в другом проекте, но этот проект не работал со мной, когда я отправляю его в form_params

Работающий код жадности

$http = new Client;

                        try {
                            $response = $http->post('https://smsmisr.com/api/webapi/?username='.$this->username.'&password='.$this->password.'&language=1&sender='.$this->sender.'&mobile=XXXX&message=Hello&DelayUntil='.Carbon::now()->toDateTimeString());

                            // retrun json_decode((string)) $response->getBody(), true);
                            return $response->getBody();

                        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
                            if($e->getCode() === 400) {
                                return response()->json('Invalid Request.', $e->getCode());
                            } else if ($e->getCode() === 401) {
                                return response()->json('Your username and passowrd are incorrect', $e->getCode());
                            }
                            return response()->json('Something went wrong on the server', $e->getCode());
                        }

Не работает код жадности код no еще не отправляет никаких форм_параметров.

$response = $http->post($this->link, [
    'headers' => [
        'User-Agent' => 'testing/1.0',
        'Accept'     => 'application/json',
        'X-Foo'      => ['Bar', 'Baz']
    ],
    'form_params' => [
        'username' => $this->username,
        'password' => $this->password,
        'sender' => $this->sender,
        'language' => 1,
        'mobile' => 'XXXXXXX',
        'message' => 'Hello guys',
        'DelayUntil' => Carbon::now()->toDateString()
    ]
]);

Эта проблема также возникает в моем Vuejs, когда я использую Axios. Я должен выполнить s

ubmitForm(context, data) {
            const params = {
                ...data
            }
            return new Promise((resolve, reject) => {
                axios.post(`${data.post.apiURL}`, params)
                    .then(response => {
                        resolve(response)
                    })
                    .catch(error => {
                        reject(error)
                    })
            })
        },

I для тестирования на PostMan.

Ответы [ 2 ]

0 голосов
/ 07 февраля 2019

Попробуйте, братан, замените "form_params" на "JSON"

    $response = $http->post($this->link, [
'headers' => [
    'User-Agent' => 'testing/1.0',
    'Accept'     => 'application/json',
    'X-Foo'      => ['Bar', 'Baz']
],
'json' => [
    'username' => $this->username,
    'password' => $this->password,
    'sender' => $this->sender,
    'language' => 1,
    'mobile' => 'XXXXXXX',
    'message' => 'Hello guys',
    'DelayUntil' => Carbon::now()->toDateString()
] ]);
0 голосов
/ 07 февраля 2019

Из документов здесь

form_params нельзя использовать с json:

form_params нельзя использовать с опцией multipart.Вам нужно будет использовать один или другой.Используйте form_params для запросов application / x-www-form-urlencoded и multipart для запросов multipart / form-data.

Этот параметр нельзя использовать с body, multipart или json

...