How to handle HTTP_PUT? #158
-
Hi, Here is a link to the existing code: From what I understand, Which leaves us to only use void init()
{
server.on("^\\/upload\\/([^/]+)$", HTTP_PUT, handleUploadPUT);
}
void handleUploadPUT(AsyncWebServerRequest *request)
{
String path = request->pathArg(0);
[...]
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Where do you see that ? I saw no such thing in the code, plus, the HTTP spec supports a body, even for GET requests strangely. |
Beta Was this translation helpful? Give feedback.
-
[[EDIT: I was writting this answer 10min after the OP. But I had troubles validating that it works. ]] I guess I misread the source code of Here is a simple snippet for future references: #include <ESPAsyncWebServer.h>
void init() {
server.on("^/upload/(.+)$", HTTP_PUT, handleUpload, nullptr, handleUploadPUT);
}
struct HandlePUTStates {
File dataFile;
};
void handleUploadPUT(AsyncWebServerRequest *request, uint8_t* data, size_t len, size_t index, size_t total)
{
String path = request->pathArg(0);
struct HandlePUTStates * states;
if (index == 0) {
states = new HandlePUTStates();
states->dataFile = File();
request->_tempObject = states;
File dataFile = fileSystem.open(path, FILE_WRITE);
states->dataFile = dataFile;
} else {
states = (struct HandlePUTStates *) request->_tempObject;
}
states->dataFile.write(data, len);
if(index + len == total) {
states->dataFile.close();
states->dataFile = File();
}
} I am just missing this piece: case RAW_ABORTED:
dataFile.close();
fileSystem.remove(path);
log_v("Upload PUT: file aborted");
break; I did not find yet a way to detect a request abort. |
Beta Was this translation helpful? Give feedback.
aborting a request from client-side would close the connection thus trigger the disconnect clalback.
did you try
request->onDisconnect([]() { ... })
?Never tried, but it is implemented.