routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

@UploadedFile and @Body

Open ssljivic opened this issue 9 years ago • 9 comments

Is it possible and how to combine @UploadedFile and @Body in the same endpoint?

From what I tested, @UploadedFile and @BodyParam works fine, but with @UploadedFile and @Body the error:

"ParameterParseJsonError: Given parameter undefined is invalid. Value ({"foo":"123456","bar":"123456"}) cannot be parsed to JSON'".

ssljivic avatar May 19 '17 14:05 ssljivic

ParameterParseJsonError thrown then given parameter value (body value) cannot be parsed into json.

protected parseValue(value: any, paramMetadata: ParamMetadata): any {
        if (typeof value === "string") {
            try {
                return JSON.parse(value);
            } catch (error) {
                throw new ParameterParseJsonError(paramMetadata.name, value);
            }
        }

        return value;
    }

See here. Try to debug and console.log value of value variable and why JSON.parse cannot handle it.

pleerock avatar May 22 '17 04:05 pleerock

@pleerock the value is already an object (request.body), so JSON.parse() is not invoked at all. However, the root exception is in the class-validator\metadata\MetadataStorage:

if (metadata.target instanceof Function &&
                !(targetConstructor.prototype instanceof metadata.target))

because targetConstructor is undefined.

ssljivic avatar May 22 '17 09:05 ssljivic

@pleerock , I have a similar error. Since 7.0, I cannot upload files via multer and access the body() object at the same time. I'm getting

TypeError: Cannot read property 'prototype' of undefined
    at /home/user/project/watch/web/src/metadata/MetadataStorage.ts:79:36
    at Array.filter (native)
    at MetadataStorage.getTargetValidationMetadatas (/home/user/project/watch/web/src/metadata/MetadataStorage.ts:75:61)
    at ValidationExecutor.execute (/home/user/project/watch/web/src/validation/ValidationExecutor.ts:44:54)
    at Validator.coreValidate (/home/user/project/watch/web/src/validation/Validator.ts:31:18)
    at Validator.<anonymous> (/home/user/project/watch/web/src/validation/Validator.ts:73:35)
    at step (/home/user/project/watch/web/node_modules/class-validator/validation/Validator.js:32:23)
    at Object.next (/home/user/project/watch/web/node_modules/class-validator/validation/Validator.js:13:53)
    at /home/user/project/watch/web/node_modules/class-validator/validation/Validator.js:7:71
    at __awaiter (/home/user/project/watch/web/node_modules/class-validator/validation/Validator.js:3:12)
    at Validator.validateOrReject (/home/user/project/watch/web/node_modules/class-validator/validation/Validator.js:75:16)
    at Object.validateOrReject (/home/user/project/watch/web/src/index.ts:71:44)
    at ActionParameterHandler.validateValue (/home/user/project/watch/web/src/ActionParameterHandler.ts:175:20)
    at ActionParameterHandler.normalizeParamValue (/home/user/project/watch/web/src/ActionParameterHandler.ts:132:34)
    at ActionParameterHandler.handle (/home/user/project/watch/web/src/ActionParameterHandler.ts:44:28)
    at /home/user/project/watch/web/src/RoutingControllers.ts:116:49
    at Array.map (native)
    at RoutingControllers.executeAction (/home/user/project/watch/web/src/RoutingControllers.ts:116:14)
    at /home/user/project/watch/web/src/RoutingControllers.ts:82:33
    at routeHandler (/home/user/project/watch/web/src/driver/express/ExpressDriver.ts:143:20)
    at Layer.handle [as handle_request] (/home/user/project/watch/web/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/user/project/watch/web/node_modules/express/lib/router/route.js:131:13)

I got to use BodyParam('json') to access the post body.

laukaichung avatar May 30 '17 12:05 laukaichung

I can't say from your logs that you are using latest routing-controllers version. There is no MetadataStorage class in the library.

pleerock avatar Jun 01 '17 09:06 pleerock

I got to use BodyParam('json') to access the post body.

Because it's how you should handle it. multipart/form-data works as an array of key-value, you can't just send file and json as a normal body, you have to put the file under the key and the json value as an another key. What should @Body() return when you send file and json? Can you show me the example in pure express where you send file and json and you have it parsed directly in req.body?

MichalLytek avatar Jun 05 '17 09:06 MichalLytek

I have the same problem as stonecold123. Below code throw the exactly same error.

export class Query {
  @IsInt() offset: number
  @IsInt() limit: number
  @IsString() sortby: string
  @IsString() order: string
}

@Controller('/v1/problems')
export class PostsController {
  @Get('/')
  async index (@Ctx() ctx: Context, @QueryParams() query: Query) {
    const problems = await ctx.services.problems.list(query.offset, query.limit, query.sortby, query.order)
    ctx.ok(problems)
  }
}
TypeError: Cannot read property 'prototype' of undefined
       at /home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/metadata/MetadataStorage.ts:79:36
       at Array.filter (native)
       at MetadataStorage.getTargetValidationMetadatas (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/metadata/MetadataStorage.ts:75:61)
       at ValidationExecutor.execute (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/validation/ValidationExecutor.ts:44:54)
       at Validator.coreValidate (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/validation/Validator.ts:31:18)
       at Validator.<anonymous> (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/validation/Validator.ts:73:35)
       at step (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/node_modules/class-validator/validation/Validator.js:32:23)
       at Object.next (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/node_modules/class-validator/validation/Validator.js:13:53)
       at /home/ruiming/Documents/onlinejudge/onlineJudge-backend/node_modules/class-validator/validation/Validator.js:7:71
       at Promise (<anonymous>)
       at __awaiter (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/node_modules/class-validator/validation/Validator.js:3:12)
       at Validator.validateOrReject (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/node_modules/class-validator/validation/Validator.js:75:16)
       at Object.validateOrReject (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/index.ts:71:44)
       at ActionParameterHandler.validateValue (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/ActionParameterHandler.ts:175:20)
       at ActionParameterHandler.normalizeParamValue (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/ActionParameterHandler.ts:132:34)
       at ActionParameterHandler.handle (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/ActionParameterHandler.ts:44:28)
       at /home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/RoutingControllers.ts:116:49
       at Array.map (native)
       at RoutingControllers.executeAction (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/RoutingControllers.ts:116:14)
       at /home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/RoutingControllers.ts:82:33
       at routeHandler (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/src/driver/koa/KoaDriver.ts:123:20)
       at dispatch (/home/ruiming/Documents/onlinejudge/onlineJudge-backend/node_modules/koa-compose/index.js:44:32) }
setting status code:  400

Is there any wrong of my code? My enviroment: class-validator: 0.7.0 routing-controllers: 0.7.0 npm: 5.0.2 node: 8.0.0 ts-target: es6

ruiming avatar Jun 07 '17 11:06 ruiming

@ruiming Your problem is currently covered with issue #227.

@ssljivic I'm closing the issue as it's working as intended. multipart/form-data can be handled only by @BodyParam and @UploadedFile combo.

MichalLytek avatar Aug 07 '17 17:08 MichalLytek

Can you give the full soure code ? I am getting stuck with using this snippet code

jeffhieun avatar Aug 12 '18 03:08 jeffhieun

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Dec 14 '20 00:12 github-actions[bot]