Coder Social home page Coder Social logo

Comments (3)

danielgtaylor avatar danielgtaylor commented on May 16, 2024

For non structured (not JSON/CBOR) input you can use an input byte stream, see https://github.com/danielgtaylor/huma#input-streaming. Example:

app.Resource("/stream").Post("stream-example", "docs...",
	responses.Created(),
	responses.InternalServerError(),
).Run(func(ctx huma.Context, input struct {
	ContentType string `header:"Content-Type"`
	Body        io.Reader
}) {
	contents, err := ioutil.ReadAll(input.Body)
	if err != nil {
		ctx.WriteError(http.StatusInternalServerError, "Error reading input")
		return
	}

	fmt.Printf("Got %s input: %s\n", input.ContentType, string(contents))
	ctx.WriteHeader(http.StatusNoContent)
})

This can be called like:

# Curl example:
$ curl -XPOST :8888/stream -H 'Content-Type: my/custom-type' -d 'this is a test'

# Restish example:
$ echo 'this is a test' | restish post :8888/stream -H 'Content-Type: my/custom-type'

As for forms, http.Request.Form is not currently exposed, but you can access it via an input resolver:

type FormInput struct {
	Form url.Values
}

func (f *FormInput) Resolve(ctx huma.Context, r *http.Request) {
	// TODO: Determine if the form should be parsed or not
	r.ParseForm()
	f.Form = r.PostForm
}

Then use FormInput to compose your input params and you should be able to access input.Form.

Also don't forget to set the request body size limit appropriately for your expected input sizes.

from huma.

ljg-cqu avatar ljg-cqu commented on May 16, 2024

Thank you for patient reply. I can now upload files from Postman with io.Reader, but it seams that is no button for file upload on Swagger UI.

from huma.

danielgtaylor avatar danielgtaylor commented on May 16, 2024

Interesting, this isn't something I've had to handle before from the generated docs UI. It looks like it is possible to convince Swagger UI to show the button, e.g. see https://swagger.io/docs/specification/describing-request-body/file-upload/. Huma doesn't directly support setting that up via struct tags, but you can experiment with https://github.com/danielgtaylor/huma/#custom-openapi-fields to get it working.

I'm open to ideas how to make this easier within Huma itself since it looks like both Swagger UI and RapiDoc seem to support the feature. What's the ideal way to make this work?

from huma.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.