转换卷曲狂饮POST与--form-PARAMS和--header

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

我正与一个给定的卷曲请求,我要处理超过狂饮挣扎。

卷曲请求如下:

curl --location --request POST "https://apis.myrest.com" \
  --header "Content-Type: multipart/form-data" \
  --header "Authorization: Bearer YOUR-BEARER-TOKEN" \
  --form "mediaUrl=https://myfile.mpg" \
  --form "configuration={
    \"speechModel\": { \"language\": \"en-US\" },
    \"publish\": {
      \"callbacks\": [{        
      \"url\" : \"https://example.org/callback\"
    }]
  }
}

我希望它通过这样的狂饮派:

// 1. build guzzle client:
//----------------------------------------------------------------------
$this->client = new Client([
    'base_uri' => $this->config->getBaseUri(),
]);

// 2. build guzzle request:
//----------------------------------------------------------------------
$request = new Request(
    'POST',
    'myendpoint',
    [
        'authorization' => 'Bearer ' . $this->config->getApiToken(),
        'cache-control' => 'no-cache',
        'content-type' => 'application/json',

        // maybe here, or not?
        form_params => ['mediaUrl' => 'www.media.com'],
    ]
);

// 3. send via client
//----------------------------------------------------------------------
response = $this->client->send($request, ['timeout' => self::TIMEOUT]);

我现在的问题是,我不知道如何来处理这个问题。在狂饮的文档,我发现“form_params”:http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request#post-form-requests

但它似乎并没有工作。如果我的form_params阵列添加到我的请求,接收方没有得到他们。谁能告诉我,怎么写与狂饮确切的卷曲的命令?

谢谢

forms curl parameters header guzzle
1个回答
1
投票

尝试使用multipart代替form_params的。

http://docs.guzzlephp.org/en/latest/request-options.html#form-params

从狂饮文档:

form_params不能与多选项一起使用。您将需要使用一个或另一个。使用form_params应用/的X WWW的形式,进行了urlencoded请求,并为多的multipart / form-data的请求。

此外尝试设置狂饮客户端与debug,因为它会显示其发送的原始HTTP请求,这样你就可以使用curl命令更容易进行比较。

http://docs.guzzlephp.org/en/latest/request-options.html#debug

这是很难理解什么是你想发送确切的请求,因为卷曲例子和你的代码之间incosistencies。我想是最好的,我可以复制卷曲。请注意,Request第三个参数只要求报头,并请求选项,你必须使用send的第二个参数。

$client = new Client([
    'base_uri' => 'https://example.org',
    'http_errors' => false
]);

$request = new Request(
    'POST',
    '/test',
    [
        'Authorization' => 'Bearer 19237192837129387',
        'Content-Type' => 'multipart/form-data',
    ]
);

$response = $client->send($request, [
    'timeout' => 10,
    'debug' => true,
    'multipart' => [
        [
            'name'     => 'mediaUrl',
            'contents' => 'https://myfile.mpg'
        ],
        [
            'name'     => 'configuration',
            'contents' => json_encode([
                'speechModel' => [
                    'language' => 'en-US'
                ],
                'publish' => [
                    'callbacks' =>
                        [
                            [
                                'url' => 'https://example.org/callback'
                            ]
                        ]
                ]
            ])
        ]
    ]
]);
© www.soinside.com 2019 - 2024. All rights reserved.