AndroidAsync
AndroidAsync copied to clipboard
Request body in multipart POST is empty
I have a simple server in android that I'm trying to access from my PC. I have setup the endpoints like this
server.get("/status", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
Log.d(TAG, "GET /status");
response.send("Ok");
}
});
server.post("/scan", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
Log.d(TAG, "POST /scan");
if (request.getBody() instanceof MultipartFormDataBody) {
MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
}
response.send("Scanned");
}
});
I placed a breakpoint inside post request handler and the formData of the request body is always null. I've been unable to access the file submitted in this POST request. I made my request using curl with the command below
curl -F 'filename=@/Users/l4rry/test/names.txt' http://192.168.1.178:8080/scan
I'm I doing something wrong? How can I get multipart POST to work?
Has this problem been solved?
Solved
- Front End: Vue 3 + vue-resource
let metaInfo = {
name: "metainfo",
id: "123",
color: ["red", "blue"],
}
const formData = new FormData();
Object.keys(metaInfo).forEach(key => {
formData.append(key, metaInfo[key]);
})
this.$http.post('http://192.168.43.1:5050/test/', formData).then(
(response) => console.log(response),
(error) => console.log(error)
);
- AndroidAsync Server End : the
postmethod which listen to special path regex should contain some code below: https://github.com/koush/AndroidAsync/blob/d0042f720c557a27a963553dae1e0ea58d00400f/AndroidAsync/test/src/com/koushikdutta/async/test/MultipartTests.java#L48-L73
NOTE
- Front End:
- please DO NOT use
axios,URLSearchParamsorxmlhttprequestto post info; - if the object appended in formData is complex structure, you should append each key in the formData but do not just append the whole object in it, which cause
Server Get Field is [Object Object]problem;
- please DO NOT use
- Server End:
- maybe you should set CORS with
response.getHeaders().set("Access-Control-Allow-Origin", "*");
- maybe you should set CORS with