简体   繁体   English

Laravel在GuzzleHttp中的参数问题

[英]Laravel problem with parameter in GuzzleHttp

Hello 你好

When I use GuzzleHttp\\Client is working fine but form_params not working but It was working with me in another project but this project no params working with me when I send it in form_params 当我使用GuzzleHttp\\Client它工作正常,但form_params无法正常工作,但它在另一个项目中与我合作,但是当我在form_params其发送给我时,该项目没有任何参数与我form_params

Guzzle Code working uzz代码工作

$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());
                        }

Guzzle Code not working the code no sends any form_params yet. 食人魔代码不起作用 ,代码no尚未发送任何form_params。

$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()
    ]
]);

This problem in my Vuejs too when using Axios I should make s 在使用Axios时,我的Vuejs中也存在此问题,我应该使

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 testing on PostMan. 我在PostMan上进行测试。

From the docs here 这里的文档

form_params cannot be used with json : form_params不能与json一起使用:

form_params cannot be used with the multipart option. form_params不能与multipart选项一起使用。 You will need to use one or the other. 您将需要使用其中一个。 Use form_params for application/x-www-form-urlencoded requests, and multipart for multipart/form-data requests. 将form_params用于application / x-www-form-urlencoded请求,将multipart用于multipart / form-data请求。

This option cannot be used with body, multipart, or json 此选项不能与body,multipart或json一起使用

Try this bro, change "form_params" to "json" 试试这个兄弟,将“ 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()
] ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM