bazi bazi - 17 days ago 10
PHP Question

dropzone not uploading, 400 bad request, token_not_provided

okay i've been trying this for like 2 hrs now and cant to make this work. dropzone cant upload any file. the server says "token not provided". im using laravel as backend and it uses jwt tokens for authentication and angular as front end. here's my dropzone config.

$scope.dropzoneConfig = {
options: { // passed into the Dropzone constructor
url: 'http://localhost:8000/api/attachments'
paramName: 'file'
},
eventHandlers: {
sending: function (file, xhr, formData) {
formData.append('token', TokenHandler.getToken());
console.log('sending');
},
success: function (file, response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
}
};


and the route definition

Route::group(array('prefix' => 'api', 'middleware' => 'jwt.auth'), function() {
Route::resource('attachments', 'AttachmentController', ['only' => 'store']);
}));


and the controller method

/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(Request $request)
{
$file = Input::file('file');
return 'okay'; // just until it works
}


the token is correct and is actually getting to the server (because i tried returning the token using Input::get('token') in another controller function and it works). can someone tell me what im doing wrong? im getting "400 Bad Request" with "token_not_provided" message...
thanks for any help. and i apologize for my bad english..

Answer

I'm not sure why appending the token to the form isn't working, but you could try sending it in the authorization header instead.

Replace

formData.append('token', TokenHandler.getToken());

With

xhr.setRequestHeader('Authorization', 'Bearer: ' + TokenHandler.getToken());