koush / AndroidAsync

Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.
Other
7.52k stars 1.56k forks source link

Request body in multipart POST is empty #638

Open ivange94 opened 5 years ago

ivange94 commented 5 years ago

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?

TYUpya commented 5 years ago

Has this problem been solved?

meteorlin commented 3 years ago

Solved

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", "*");`