AndroidAsync icon indicating copy to clipboard operation
AndroidAsync copied to clipboard

Request body in multipart POST is empty

Open ivange94 opened this issue 7 years ago • 2 comments

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?

ivange94 avatar Jan 10 '19 16:01 ivange94

Has this problem been solved?

TYUpya avatar Mar 04 '19 09:03 TYUpya

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 post method 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, URLSearchParams or xmlhttprequest to 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;
  • Server End:
    • maybe you should set CORS with response.getHeaders().set("Access-Control-Allow-Origin", "*");

meteorlin avatar Dec 08 '20 14:12 meteorlin