Open lHondol opened 7 months ago
Having similar trouble here using try-it-out. Route uses HTTP PUT method and has a "file" parameter named "photo". php artisan scribe:generate
generates "correct" documentation, I can pick a file and fill other parameters. If no file is selected, everything works fine, only when providing it, it is missing as described by the OP.
{"photo":{}}
The request header, as per the browser developer tools is not changed from "Content-Type: application/json"
If I change the header manually to "multipart/form-data", the validation logic passes (the file is not mandatory), but the try-it-out does not send anything multipart encoded (file is missing), nothing is done at all regarding the file. The correct (manually changed) Content-Type is sent, but (if I am not understanding something wrong), it should be deleted (line 192)
Edit: version here is 4.25.0
Thanks for your preliminary investigation. I'll look at this some more when I have the time, but in the meantime, feel free to submit a PR if you have an idea what needs to be fixed.
When uploading a file the Content-Type
header must not be set otherwise the browser cannot set the right boundaries for the multipart formdata payload.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest_API/Using_FormData_Objects
You can add following code to index.blade.php
of vendor published elements template in line :126
to get it to work
// ...
let preflightPromise = Promise.resolve();
if (window.useCsrf && window.csrfUrl) {
preflightPromise = makeAPICall('GET', window.csrfUrl).then(() => {
headers['X-XSRF-TOKEN'] = getCookie('XSRF-TOKEN');
});
}
// START this needs to be added
if (form.dataset.hasfiles === "1") {
delete headers['Content-Type'];
}
// END this needs to be added
return preflightPromise.then(() => makeAPICall(method, path, body, query, headers, endpointId))
// ...
Can you make a PR with that fix?
Scribe version
4.23
PHP version
8.2.4
Framework
Laravel
Framework version
10
Scribe config
What happened?
In my FormRequest, i have these rules:
In the generated documentation, within the "try_out" body, I can now choose the file I need to upload. However, when I hit submit, the response states, 'The information field is required,' even though I have already provided the value.
also the request payload:
request headers : Content-Type: 'multipart/form-data' Accept: 'application/json'
Docs