如何在翻新中发送多部分/表单数据?

时间:2019-07-11 13:26:50

标签: android node.js retrofit retrofit2 multipartform-data

我使用nodejs编写了REST API。一些请求以multipart / form-data的形式发送到我的端点。因此,我在npm上使用了multer库。

当我从邮递员发送请求时,我的端点正在工作。但是我无法在android请求中获取正文。 Resuest主体将空对象发送到我的服务器。Android正在使用http请求的改型库。

我的终点:


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);
    }
})

我的Kotlin代码:

  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>

0 个答案:

没有答案