GuzzleHttp \\ Exception \\ ClientException:客户端错误:`POST导致`400 Bad Request`响应:\ n {“ @ context”:“ \\ / api \\ / contexts

问题描述 投票:0回答:1

我已使用本教程https://symfonycasts.com/screencast/api-platform构建了API端点。我已经从Web界面测试了API,它接受输入并存储数据。现在,我正在尝试将数据从应用程序发送到端点。

 curl -X POST "https://myweblocation.app/api/emergencyvisits" -H "accept: application/ld+json" -H "Content-Type: application/json" -d "{\"externalpatientid\":\"6244f921d8960a6027960d96b4806a46\",\"externalsiteid\":\"3fbe982d-2902-45d7-ad30-5bc4f17f92b4\",\"poscode\":20,\"dos\":\"2020-02-28T00:10:52.416Z\",\"vistreason\":\"chest hurting bad\"}"

我的代码是这样:

    $client = new Client(['verify' => 'my/pem/location.pem' ]);
    $siteid = $GLOBALS['unique_installation_id'];

    $body = [
        'externalpatientid' => $uuid,
        'externalsiteid' => $siteid,
        'poscode' => $pos_code,
        'dos' => $date,
        'visitreason' => $reason
    ];

    $headers = [
    'content-type' => 'application/json',
        'accept' => 'application/ld+json'
    ];

    $request = new Request('POST', 'https://gencyehr.app/api/emergencyvisits', $headers, json_encode($body));

    $response = $client->send($request, ['timeout' => 2]);

如何使Guzzle以编程方式向服务器生成正确的帖子?

php api-platform.com symfony5
1个回答
0
投票

首先,请不要发布敏感数据,例如患者ID,站点ID或您的应用程序URL。

关于您的问题...在curl命令中,使用参数名称vistreason,但在Guzzle请求中,使用visitreason

我已经用Postman进行了测试,由于字段vistreason不能为null,它返回了500服务器错误。

此外,我还用Guzzle(6.x)进行了测试:

$client = new Client(['verify' => false]); // I deactivated ssl verification
$body = [
    'externalpatientid' => '<id from curl request>',
    'externalsiteid' => '<id from curl request>',
    'poscode' => 20,
    'dos' => '2020-02-28T00:10:52.416Z',
    'vistreason' => 'chest hurting bad'
];

$response = $client->request(
    'POST',
    '<your-server-api-url>',
    [
        'headers' => [
            'content-type' => 'application/json',
            'accept' => 'application/ld+json'
        ],
        'body' => json_encode($body),
    ]
);

var_dump(json_decode($response->getBody()->getContents()));
// Output seems to be a valid response with some data from the request.

也许您的验证证书有问题。

© www.soinside.com 2019 - 2024. All rights reserved.