mehmet
mehmet

Reputation: 41

How can I create the curl request exactly as a guzzle?

I have a curl request like below I'm trying to convert it to guzzle but when I send a request to cloudflare it keeps returning me an error. "decoding error" Is there a bug in my guzzle request? Normal curl request should be stable.

`curl \
-X POST \
-d '{"url":"https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4","meta":{"name":"My First Stream Video"}}' \
-H "Authorization: Bearer <API_TOKEN>" \
https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/stream/copy`

My codes are available below.

 $response = $client->request('POST', 'https://api.cloudflare.com/client/v4/accounts/' . $accountId . '/stream/copy', [
                    'headers' => [
                        'Authorization' => 'Bearer ' . $token,
                    ],

    'form_params' => [
        "url" => "https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4",
        "meta" => [
            "name": "My First Stream Video"
        ]
    ]

Upvotes: 0

Views: 315

Answers (3)

mehmet
mehmet

Reputation: 41

This is how I found the solution. If anyone has problems, they can use this.

'headers' => [
    'Authorization' => 'Bearer ' . $token,
    'Content-Type' => 'application/json',
],
'json' => [
    "url" => "https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4",
    "meta" => [
        "name": "My First Stream Video"
    ]
]

Upvotes: 0

Lucky Person
Lucky Person

Reputation: 243

This answer is using Laravel's HTTP request.

Http::withBody('{"url":"https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4","meta":{"name":"My First Stream Video"}}')
    ->withToken('<API_TOKEN>')
    ->post('https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/stream/copy');

Also, check this amazing tool owned by Shift

https://laravelshift.com/convert-curl-to-http

Upvotes: 0

Harshana
Harshana

Reputation: 5496

Try setting the content type header as well.

$response = $client->request('POST', 'https://api.cloudflare.com/client/v4/accounts/' . $accountId . '/stream/copy', [
                    'headers' => [
                        'Content-Type' => 'application/json',
                        'Authorization' => 'Bearer ' . $token,
                    ],

    'form_params' => [
        "url" => "https://storage.googleapis.com/zaid-test/Watermarks%20Demo/cf-ad-original.mp4",
        "meta" => [
            "name": "My First Stream Video"
        ]
    ]

Upvotes: 1

Related Questions