Upload Api returns 401 Unsupported Media Type

I am using Laravel to query the Box Api endpoints. I am specifically getting an error on the Box File upload api.
Here is the piece of code that I am using:

            ->post($this->uploadUrl, $attributes);

Here is curl equivalent of the same:

curl -H 'Content-Type: multipart/form-data'  \
-H 'Authorization: Bearer {Token}' -d 'attributes%5Bname%5D=test.pdf&attributes%5Bparent%5D%5Bid%5D=12345&file=test.pdf' -X 'POST' 'https://upload.box.com/api/2.0/files/content'

Please help.

Hello :wave: Thanks for posting on the forum.

I’m not familiar with Laravel… but I based on your CURL snippet something looks to be different from our posted snippet on the curl repo.

We also have a guide here that talks more about what the multipart/form-data should look like.

I did send a question to chatgpt and got this response… not sure if this is helpful or not

You can interact with the Box API directly using Laravel’s built-in HTTP client or any other HTTP client library of your choice. Here’s how you can upload a file to Box without using the SDK:

Step 1: Set up your Laravel project and install required libraries.

Make sure you have a Laravel project set up, and install any required libraries for handling file uploads and making HTTP requests. For example, you can use the Laravel HTTP Client, which is included by default in Laravel:

composer require guzzlehttp/guzzle

Step 2: Obtain Box API credentials.

Go to the Box Developer Console (https://developer.box.com/) and create a new application. Retrieve the Client ID, Client Secret, and Developer Token. Store these credentials in your Laravel .env file:


Step 3: Create a file upload route and controller.

As we did before, create a new route and controller to handle the file upload to Box:

// routes/web.php
Route::post('/upload-to-box', 'BoxController@upload');
php artisan make:controller BoxController

Step 4: Implement the file upload logic using Laravel HTTP Client.

In the BoxController, implement the upload method:

// app/Http/Controllers/BoxController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class BoxController extends Controller
    public function upload(Request $request)
        $clientId = config('app.box_client_id');
        $clientSecret = config('app.box_client_secret');
        $developerToken = config('app.box_developer_token');
        $folderId = 'YOUR_TARGET_FOLDER_ID'; // Replace this with the target folder ID in your Box account

        $fileContent = $request->file('file')->get();
        $fileName = $request->file('file')->getClientOriginalName();

        $response = Http::withHeaders([
            'Authorization' => 'Bearer ' . $developerToken,
            'Content-Type' => 'multipart/form-data',
        ])->post("https://upload.box.com/api/2.0/files/content", [
            'attributes' => json_encode(['name' => $fileName, 'parent' => ['id' => $folderId]]),
            'file' => $fileContent,

        $fileData = $response->json();

        // You can do further processing or return a response as needed
        return response()->json(['message' => 'File uploaded successfully.', 'file_id' => $fileData['id']]);

Step 5: Handle the file upload in your frontend.

As before, create a form in your frontend application to upload the file to your Laravel backend. Ensure the form’s enctype attribute is set to "multipart/form-data".

Step 6: Test the file upload.

When you submit the file upload form, it will send the file to the Laravel backend. The backend will then make a request to the Box API and upload the file to your Box account using the Laravel HTTP client.

Please note that using the Box API directly without an SDK can be more complex, and you’ll need to handle various aspects of the integration, such as authentication, error handling, and formatting of requests and responses. Additionally, you may need to implement additional features, like refreshing access tokens, depending on your requirements. If possible, using the Box SDK or another Box API client library can simplify the process and handle these aspects for you.

Alex, Box Developer Advocate :avocado: