skip to Main Content

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"
        ]
    ]

3

Answers


  1. Chosen as BEST ANSWER

    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"
        ]
    ]
    

  2. 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"
            ]
        ]
    
    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search