0

I wrote rest api using nodejs. Some requests come in multipart/form-data to my endpoints. Therefore i used multer library on npm.

When i send the request from postman my endpoint is working.But I can't get the body on android request. Resuest body comes empty object to my server.Android is using retrofit library for http requests.

my endpoint:


app.post('/registration',isAuth,isPartner,upload.array('files', 5),(req, res)=>{
    try {
        const regist = req.body
        const files = req.files
        // regist.partner_id = req.query.user_id || "0c6f5567-8d95-492f-b3e5-741c496a7c65";
        const user_id = req.user.id;
        const user_role_id = req.user.role_id;
        if(user_role_id === 1){
            regist.partner_id = user_id;
        }else if (user_role_id === 2 || user_role_id === 3 || user_role_id === 4 ){
            regist.manager_id = user_id;
            regist.partner_id = req.user.user_id;
        }
        if(files){
            regist.files = files.map((item) => {
                return item.path;
            });
        }
        regist.updated_at = Date.now();
        request(config.blockChain['url'] + '/?type=Invoke&userID=' + regist.partner_id + '&ip=1&key=1&tutar=1&aboneId=1',{json:true},(err,response,body) => {
            if(body.transaction_id){
                regist.transaction_id = body.transaction_id;
            }else {
                throw "Transaction can not create.Try again"
            }
            // if solution id equal null
            if(regist.solution_id == null){
                db.solutions.create({text:regist.solution_text},{returning:true})
                .then((solution)=>{
                    regist.solution_id = solution.id;
                    return regist;
                })
                .then((data) => {
                    db.registrations.create(data)
                    .then( registration => {
                        const response = {
                            code:200,
                            status:true,
                            msg:"Success",
                            data:registration
                        }
                        res.status(response.code).json(response);
                    })
                })
                .catch(err => {
                    response = {
                        code:404,
                        status:false,
                        msg:err.stack
                    }
                    res.status(response.code).json(response);
                })
        }
        else {
            // if have a solution_id
            db.registrations.create(regist)
            .then((data)=>{
                response = {
                    code:200,
                    status:true,
                    msg:"Success",
                    data:data
                }
                res.status(response.code).json(response)
            })
            .catch(err => {
                response = {
                    code:404,
                    status:false,
                    msg:err.stack
                }
                res.status(response.code).json(response);
            });
        }
    })

    }
  catch (err) {
        response = {
            code:500,
            status: false,
            msg: err.stack
        }
        res.status(response.code).json(response);
    }
})

my kotlin code:

  val gson = Gson()
  val addRegistrationJson = gson.toJson(addRegistration)!!

  val requestBody = MultipartBody.Builder()
         .setType(MultipartBody.FORM)
         .addFormDataPart("body",addRegistrationJson).build()

 @Multipart
 @POST("registration")
 fun addRegistration2(@Header("authorization") authHeader:String,@Part("body") body:RequestBody ):Call<ResponseMessage>
Jaime Suarez
  • 660
  • 4
  • 11
ByBarov
  • 1
  • 3
  • Please, post your android code. – Jaime Suarez Jul 11 '19 at 13:29
  • i added kotlin code. i can get body now as string.Normally i can get body as json when i send request from postman.But i can not as json on kotlin @JaimeSuarez – ByBarov Jul 12 '19 at 07:34
  • I think you are not sending the body in the correct way. Please, post the object `addRegistration` along with the fields the endpoint requires. – Jaime Suarez Jul 12 '19 at 08:41
  • Thanks @JaimeSuarez I will say to my teammate.Actually the android part she wrote. I do not know the mobile.I thought it was my backend code. – ByBarov Jul 12 '19 at 13:48
  • @JaimeSuarez Can you help? https://stackoverflow.com/questions/62783444/why-does-multipart-pdf-is-not-able-to-upload-in-api-using-retrofit-2-in-android?noredirect=1#comment111031344_62783444 – Priyanka Singh Jul 08 '20 at 07:37

0 Answers0