How to use the co-body.form function in co-body

To help you get started, we’ve selected a few co-body examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Summerlve / koablog / controllers / User.js View on Github external
module.exports.add = function* add (next) {
    let body = yield parse.form(this);

    // create a new user
    let username = body.username;
    let password = body.password;
    let penName = body.penName;
    let groupName = body.groupName;
    let avatar = body.avatar;
    let introduce = body.introduce;

    if (!username || !password || !penName || !groupName) {
        this.status = 400;
        this.body = {
            statusCode: 400,
            reasonPhrase: "Bad Request",
            description: "username, password, penName, groupName is required, and must be not void",
            errorCode: 2000
github koajs / joi-router / joi-router.js View on Github external
return async function parseFormBody(ctx, next) {
    if (!ctx.request.is('urlencoded')) {
      return ctx.throw(400, 'expected x-www-form-urlencoded');
    }

    // eslint-disable-next-line require-atomic-updates
    ctx.request.body = ctx.request.body || await parse.form(ctx, opts);
    await next();
  };
}
github Summerlve / koablog / controllers / Group.js View on Github external
module.exports.add = function* add (next) {
    let body = yield parse.form(this);

    let groupName = body.groupName;
    let description = body.description;

    if (!groupName) {
        // groupName can not be void
        this.status = 400;
        this.body = {
            statusCode: 400,
            reasonPhrase: "Bad Request",
            description: "groupName can not be void",
            errorCode: 5000
        };
        return ;
    }
github Summerlve / koablog / controllers / User.js View on Github external
module.exports.update = function* update (next) {
    // get user from checkUser
    let user = this.user;

    let body = yield parse.form(this);

    // check which field need to update
    let username = "";
    let password = "";
    let penName = "";
    let introduce = "";

    let updater = {};

    if ("username" in body) {
        username = body.username;

        if (!username) {
            this.status = 400;
            this.body = {
                statusCode: 400,
github adonisjs / adonis-middleware / src / BodyParser / index.js View on Github external
* _parse (request) {
    let formBody = {
      fields: {},
      files: {},
      raw: null
    }

    if (request.is(contentTypes.json)) {
      formBody.fields = yield coBody.json(request.request, this.formOptions)
    } else if (request.is(contentTypes.form)) {
      formBody.fields = yield coBody.form(request.request, this.formOptions)
    } else if (request.is(contentTypes.text)) {
      formBody.raw = yield coBody.text(request.request, this.formOptions)
    } else if (request.is(contentTypes.multipart)) {
      formBody = yield this._multipart(request, this.uploadOptions)
    }
    return formBody
  }
github adonisjs / adonis-framework / packages / bodyparser / src / BodyParser / index.ts View on Github external
return next()
      }

      const { files, fields } = await processMultipart(request['multipart'], multipartConfig)
      request.setInitialBody(fields)
      request['_files'] = files
      return next()
    }

    /**
     * Handle url-encoded form data
     */
    const formConfig = this._getConfigFor('form')
    if (this._isType(request, formConfig.types)) {
      try {
        const { parsed, raw } = await coBody.form(request.request, formConfig)
        request.setInitialBody(parsed)
        request.updateRawBody(raw)
        return next()
      } catch (error) {
        throw this._getExceptionFor(error)
      }
    }

    /**
     * Handle content with JSON types
     */
    const jsonConfig = this._getConfigFor('json')
    if (this._isType(request, jsonConfig.types)) {
      try {
        const { parsed, raw } = await coBody.json(request.request, jsonConfig)
        request.setInitialBody(parsed)
github dazejs / daze / packages / framework / src / request / utils / parse-body.ts View on Github external
export async function parseBody(request: any) {
  let body: any = {};
  if (request.is('json')) {
    body.fields = await buddy.json(request.req);
  } else if (request.is('urlencoded')) {
    body.fields = await buddy.form(request.req);
  } else if (request.is('text')) {
    body.fields = await buddy.text(request.req);
  } else if (request.is('multipart')) {
    body = await parseForm(request.req);
  }
  return body;
};
github t2ee / vader / dist / core / Router.js View on Github external
return yield new Promise((resolve, reject) => {
                    parse.form(koaContext).then(resolve).catch(reject);
                });
            }
github Summerlve / koablog / routes / tags.js View on Github external
function* (next) {
		let body = yield parse.form(this);
		let tagName = body.tagName;

		if (!tagName) {
			this.status = 400;
			this.body = {
				statusCode: 400,
				reasonPhrase: "Bad Request",
				description: "tag's name can not be void",
				errorCode: 4001
			};
			return ;
		}

		let transaction = yield sequelize.transaction();

		try {
github koajs / bodyparser / index.js View on Github external
async function parseBody(ctx) {
    if (enableJson && ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes))) {
      return await parse.json(ctx, jsonOpts);
    }
    if (enableForm && ctx.request.is(formTypes)) {
      return await parse.form(ctx, formOpts);
    }
    if (enableText && ctx.request.is(textTypes)) {
      return await parse.text(ctx, textOpts) || '';
    }
    return {};
  }
};

co-body

request body parsing for co

MIT
Latest version published 7 months ago

Package Health Score

77 / 100
Full package analysis