简体   繁体   English

Guzzle 422无法处理的实体错误

[英]Guzzle 422 Unprocessable Entity error

I'm using browserstack screenshots API - https://www.browserstack.com/screenshots/api The following curl is working: 我正在使用browserstack屏幕截图API- https ://www.browserstack.com/screenshots/api以下卷曲正在工作:

curl -u "username:password" -H "Content-Type: application/json" -H "Accept: application/json"  -d '{"browsers": [{"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"}], "url": "http://google.com"}' http://www.browserstack.com/screenshots

However, when I try the same call call with guzzle I get 422 Unprocessable Entity error. 但是,当我用相同的口哨尝试相同的呼叫时,会收到422无法处理的实体错误。

$client = new GuzzleHttp\Client();
$request = $client->post('http://www.browserstack.com/screenshots', [
        'headers' => ['Content-type' => 'application/json'],
        'auth' =>  ['username', 'password']
]
);

$data = ['browsers' => ['os' => 'Windows', 'os_version' => '7', 'browser_version' => '8.0', 'browser' => 'ie'], 'url' => 'http://google.com'];
$request->setBody($data);
$response = $request->send();
dd($response);

Can you suggest how to debug this issue? 您能建议如何调试此问题吗?

To send JSON to the Screenshots API, you would need to format it as a JSON string. 要将JSON发送到Screenshots API,您需要将其格式化为JSON字符串。

$client = new GuzzleHttp\Client();
$request = $client->post('http://www.browserstack.com/screenshots', [
        'headers' => ['Content-type' => 'application/json'],
        'auth' =>  ['username', 'access_key'],
        'body' => '{"browsers": [
                           {"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"},
                           {"os": "android", "os_version": "4.4", "device": "HTC One M8", "browser": "Android Browser"}
                    ],
                    "url": "http://www.lipsum.com"}'
            ]
        );

You will then be able to view the progress on your BrowserStack Screenshots page. 然后,您将能够在BrowserStack屏幕截图页面上查看进度。

For a guide on how to send POST requests using Guzzle, you can refer this documentation — https://media.readthedocs.org/pdf/guzzle/latest/guzzle.pdf . 有关如何使用狂饮发送POST请求的指南,你可以参考这个文档- https://media.readthedocs.org/pdf/guzzle/latest/guzzle.pdf

I had a mistake in method. 我的方法有误。 It should be $client->createRequest instead of $client->post Also, I had a mistake in data being passed. 应该是$client->createRequest而不是$client->post另外,我在传递数据时有一个错误。 Browsers should be array of arrays 浏览器应为数组数组

There is another library that could be used with browserstack and guzzle: https://github.com/ksenzee/browserstack-screenshots-php 还有一个可以与browserstack和guzzle一起使用的库: https : //github.com/ksenzee/browserstack-screenshots-php

$client = new GuzzleHttp\Client();

$request = $client->createRequest('POST', 'http://www.browserstack.com/screenshots', [
        'headers' => ['Content-type' => 'application/json'],
        'auth' =>  ['user', 'pwd'],
        'body' => '{"browsers": [
                       {"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"},
                       {"os": "android", "os_version": "4.4", "device": "HTC One M8", "browser": "Android Browser"}
                ],
                "url": "http://www.lipsum.com"}'
]
);
$response = $client->send($request);
dd($response->json());

In my case uses 'json' in options's argument ( https://es.stackoverflow.com/questions/185183/porqu%C3%A9-guzzle-5-0-lanza-el-error-422-si-estoy-armando-bien-la-consulta#185192 ), example: 就我而言,在选项的参数中使用'json'( https://es.stackoverflow.com/questions/185183/porqu%C3%A9-guzzle-5-0-lanza-el-error-422-si-estoy-armando -bien-la-consulta#185192 ),例如:

$client = new GuzzleHttp\Client(['base_url' => 'http://www.browserstack.com/']);
$request = $client->post('screenshots', [
    'headers' => ['Content-type' => 'application/json'],
    'auth' =>  ['username', 'access_key'],
    'json' => '{"browsers": [
                       {"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"},
                       {"os": "android", "os_version": "4.4", "device": "HTC One M8", "browser": "Android Browser"}
                ],
                "url": "http://www.lipsum.com"}'
        ]
    );

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

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