【问题标题】:guzzle http post not working with form_paramsguzzle http post 不适用于 form_params
【发布时间】:2022-11-16 05:32:52
【问题描述】:

当我尝试发送 Guzzle-POST 时,总是返回错误:

{"errors":[{"code":"0","status":"400","title":"Bad Request","detail":"JSON 负载格式错误。"}]}

因为我没有看到任何错误,在数据数组本身内部,也许它可能是错误的标题信息?这是对我尝试添加新文章的 shopware 6 API 的简单 POST 请求。

$payload=   [
    'headers' => [
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
    ],
    'form_params' =>[
        "name" => "productname", 
        "productNumber" => "101003", 
        "stock" => 2, 
        "taxId" => "50ee15989533451095c9d7e03d9ce479", 
        "price" => [
            [
                "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca", 
                "gross" => 15, 
                "net" => 10, 
                "linked" => false 
            ] 
        ] 
    ]
];


$response = $client->request('POST', 'http://shopware6.shop.de/api/product',
    $data

);

如果我使用 Postman 或 RESTer 或类似工具,我会得到一个肯定的结果,它有效。所以我想我错过了某事。在我的 guzzle-request 中(这是来自 https://shopware.stoplight.io/docs/admin-api/ZG9jOjEyMzA4NTUy-product-data 的原始文档的副本)

我正在使用 guzzle 和 kamermans oauth2 中间件

一个简单的 GET 请求也可以工作:

    $response = $client->request('GET', 'http://shopware6.shop.de/api/product/{productid}',
        [
            'headers' => [
                'Content-Type' => 'application/json',
                'Accept' => 'application/json',

            ]
        ]
    );

【问题讨论】:

    标签: post request guzzle shopware6


    【解决方案1】:

    您在请求中遗漏了整个身份验证,您可能故意省略了它,但我认为为了完成起见,我应该在以下示例中添加它。

    除此之外,错误请求的原因是使用了密钥'form_params',它仅用于Content-Type: multipart/form-data,而不是用于负载的'json'

    $response = $client->request('POST', 'http://localhost/api/oauth/token', [
        'headers' => [
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
        ],
        'json' => [
            'grant_type' => 'client_credentials',
            'client_id' => '...',
            'client_secret' => '...',
        ],
    ]);
    $token = json_decode($response->getBody()->getContents(), true)['access_token'];
    
    $payload = [
        'headers' => [
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
            'Authorization' => 'Bearer ' . $token,
        ],
        'json' => [
            'name' => 'productname', 
            'productNumber' => '101003', 
            'stock' => 2, 
            'taxId' => '...', 
            'price' => [
                [
                    'currencyId' => '...', 
                    'gross' => 15, 
                    'net' => 10, 
                    'linked' => false,
                ], 
            ], 
        ],
    ];
    
    $response = $client->request('POST', 'http://localhost/api/product', $payload);
    

    【讨论】:

      猜你喜欢
      • 2015-11-27
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2014-07-23
      相关资源
      最近更新 更多