Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
  • 06:13
    radiantone commented #440
  • 05:15
    sillydan1 commented #440
  • May 18 14:48
    radiantone edited #440
  • May 18 14:48
    radiantone edited #440
  • May 18 14:47
    radiantone opened #440
  • May 18 14:47
    radiantone labeled #440
  • May 15 05:51
    cmpunches labeled #439
  • May 15 05:51
    cmpunches opened #439
  • May 14 08:37
    thedumbterminal edited #438
  • May 14 08:36
    thedumbterminal labeled #438
  • May 14 08:36
    thedumbterminal opened #438
  • May 14 00:07
    boompig commented #437
  • May 14 00:05
    boompig opened #437
  • May 14 00:05
    boompig labeled #437
  • May 13 17:49
    JavaScriptDude commented #116
  • May 13 12:49
    ReznikovRoman commented #135
  • May 13 12:00
    abeiertz commented #59
  • May 12 18:48
    lyonsaj commented #192
  • May 10 05:55
    busoff edited #435
  • May 07 14:12
    ankurcharan commented #151
I am able to reproduce the error now, it was due to marshal_with which seems changed returned redirection:
Here is the code:
import flask
from flask import Flask
from flask_restx import Model, Resource, fields, Namespace, abort, Api
from werkzeug.middleware.proxy_fix import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
api = Api(app, version='1.0', title='TodoMVC API',
    description='A simple TodoMVC API',

ns = Namespace('todos', description='TODO operations')
ns2 = Namespace('todos2', description='TODO operations')

todo = Model('todo', {
    'id': fields.Integer(required=True, description='The task unique identifier'),
    'task': fields.String(required=True, description='The task details'),
    'description': fields.String(required=True, description='Put some description here')

response = Model('response', {
    'id': fields.Integer,
    'task': fields.String

ns.models[todo.name] = todo
ns.models[response.name] = response

todo2 = Model('todo2', {
    'task': fields.String(required=True, description='The task details'),
    'todo_description': fields.String(required=True, description='Put some description here')

response2 = Model('response2', {
    'id': fields.Integer,
    'task': fields.String

ns2.models[todo2.name] = todo2
ns2.models[response2.name] = response


@ns2.response(404, 'Todo not found')
class Todo(Resource):
    '''Show a single todo item and lets you delete them'''
    def get(self, id):
        '''Fetch a given resource'''
        return "get todo 2"

    def post(self, id):
        '''Update a task given its identifier'''
        if id == 1:
            redirection = flask.redirect(flask.url_for('todos_todo_list', _method='POST'), code=307)
            return redirection
        return {"id": "dummy id", "task": "some dummy task here"}

class TodoList(Resource):
    '''Shows a list of all todos, and lets you POST to add new tasks'''
    def get(self):
        '''List all tasks'''
        return "get todo list"

    def post(self):
        '''Create a new task'''
        payload = ns.payload
        return {"id": payload.get("id"), "task": payload.get("task")}

if __name__ == '__main__':
John Chittum

so the requirement is:

Given an endpoint with a variable (var == $CONDITION), have a redirect only for $CONDITION, otherwise, return properly

My gut would say, if you want a single endpoint, split by condition, to do 2 redirects. One for $CONDITION, one for everything else. and the resultant endpoints handling the marshalling. Because, in the end, if their responses are going to be different, they should probably be considered separate endpoints

and that's going to be more properly rendered in the Swagger schema as well.
Yes. The reason that I set up redirection is because, the endpoint already exists to do 2 things where separated by if-else condition statement.
For right now, I want to separate the 2 things into 2 different endpoints.
In order to not introduce breaking change for our users, so I decided to make the redirection from old endpoint to the new endpoint before fully separate them.
It would be nice to not remove marshaling because the old endpoint still needed to be kept.
John Chittum

Considering you're changing core functionality based upon a condition, i'd say keeping the original endpoint with 2 redirects, split by condition. then if you want to deprecate the v1 style endpoint later, you can

the other option is to split v1 and v2 endpoints, and just tell folks to use v2 for the new setup. Versioning and moving some endpoints is normal, and versioning is supported in flask-restx. you can provide version= to the API generator.

Hi, John. Thank you for your suggestion. I think both are valid approaches. I think I am going to implement the first approach because we as a team had agree on not introducing breaking change for this new deployment and having this redirection will give users more time to adapt to the new endpoint.
Hello ,
I have a question about marshalling.
I want to use marshall_with by passing a polymorph field map in argument , but it doesn't work , can someone help me ?
I want my function to be able to marshal list of this polymorph field
Carlos PiƱa

Hi, I have a question about how can I create a field which is a fixed list of different models:

model_1 = api.model(...)
model_2 = api.model(...)
model_3 = api.model(...)

model = api.model('model', {'field': fields.List([model_1, model_2, model_3])})

I know passing the list of previous models is not expected as the first param of the fields.List class but I don't know how to achieve it. Any ideas?

Hey folks, is there any way to define an arbitrary field in an api model?
Besides going the "stringly typed" route
My first thought would have been using fields.Raw but it seems to enforce an object structure when using validate=True
e.g. it won't allow "abc" or 123, data needs to be inside a json object {}
Brock Palen
I'm trying to use values that are set in the current apps .config[] list as static values in my api.models but I'm getting errors about app_context() Rather than try to push an context in my include that has all my models are there any tips you would recomend for including configuration values when marshaling results?
Brock Palen
Ok looks like using current_app.confg[] in my models only throw the context error for setting validation values like min= max= but not when used in attribute= lambda
Abdur-Rahmaan Janhangeer
I just did this talk: Advanced Flask: Recipes For An All-weather Craft | https://www.youtube.com/watch?v=veMSbl2fbXE and mentionned Flask-restx as an example to integrate. At the end there was a question on upgrading the OpenApi spec (around 27:37). I said that we need some more helping hands
Abdur-Rahmaan Janhangeer
If anybody is up for a talk about restx would be awesome: https://flaskcon.com/y/2021/
People need to discover the framework more!
Hey all :) I have a quick question I couldn't find the answer to. I'm looking for a way to use api.doc() to document a POST request which should be able to take any JSON body (that is to say a valid JSON body is required, doesn't matter what is actually inside that body). At the moment, I am using a reqparse that is passed to the api.expect() which I dont actually use in my code but is just there to force the Swagger docs to give me a JSON body when clicking "Try it out". I understand there isn't really a way to pass custom Swagger JSON (as you would be able to pass invalid JSON) so what would be the best way to do this?
Hi, how do I change the default response content type in swagger ui. I tried ns.produces and api.representation. I woul dlike to replace application/json to application/xml
Brock Palen
Is there a way when marsharling a response to not display a field if it's null / None? I have a single code base that has nested models, and some systems don't have a given feature so rather than returning null the front end people prefer we not pass along something that doesn't exist at all on that system.
Harshal Chaudhari
@brockpalen I remember vaguely there is an option to skip none fields during marshalling. You will have to check the fields.raw and other classes used for marshalling. Alternatively, you could override the output function of any fields class and change the output to suit your needs
Brock Palen
@harshalchaudhari35 Looks like there is a mask= option that is a callable, I'll try that and report.
Harshal Chaudhari
@brockpalen here this is what I have referring to earlier
Brock Palen
@harshalchaudhari35 skip_none= is what I wanted actually, and it works perfectly becaus depending on configuration different values in the nested group are populated. In our case does our campus HPC cluster have single precision GPUs or not,
Turns out mask= does something I hacked around by having a nasty lambda function in my model so that will clean that up significantly,
Really appreciate it, I need to get in the habbit of looking at the prototypes and not just the normal example documentation,
Harshal Chaudhari
@brockpalen glad that helped and worked out for you. Yeah my experience has been the same and daunting with restx. Many times I find myself overriding many functions from the library.
Brock Palen
NP, I really like the library, just wish it had more community behind it, it front ends out 14,000 CPU HPC cluster, and will soon front end our 20PB research archive, our univeristy really appreciates such an easy to understand library is available,
Brock Palen
Maybe I'm not grocking it, but how do I input validation when for POST requests @api.route("/<string:subaccount>/user/<string:uniquename>") How do I do stricter validation of uniquename much like parsing payloads? Do I do it all manually in my routine? Or is there something more strict than <string:varname> ?
Harshal Chaudhari
Ah what you are looking for is more of base flask functionality here. Flask provides a set of converters types to validate/typecast the route params. You can create your own converter types as mentioned here. https://uniwebsidad.com/libros/explore-flask/chapter-6/url-converters
If you don't want to define your own stuff and prefer a library doing it for you heres one https://github.com/Ge0rg3/flask-parameter-validation seems to support whitelists and regex validation
Brock Palen
Thanks i'll check that out
Brock Palen
BTW do POST requests have to have a payload if all the values in the model are optional? I found my unit tests fail unless I send an empty payload.
Harshal Chaudhari
Short answer is yes :) You could get away with using content-length header set to 0 also
Brock Palen
Weird, then why do I get a 500 rc if I don't pass data=json.dumps({})
Ryan Truran
OAS3 allows for one response to have multiple schemas, through the use of oneOf, anyOf, allOf, not.
Does restx currently support this?
Matthew Jacobsen
Can anyone offer some live support right now? I am having trouble with my routing. Keep getting 404 when routing to my API Blueprint, but the regular flask routes are getting reached just fine
Jamin Collins
I'm having a bit of trouble with RequestParser and how to specify that an argument is a list of strings. From what I can find, action="append" should do it, but adding this appears to drop the element from the generated Swagger documentation.
Brock Palen
Is there a flask library to generally protect against URL injection through routes? The flask security page didn't have anything that stuck out,
@j5awry @ziirish Any ETA for the next bugfix release?
Giacomo Gritzan

I currently have problems with marshalling my Responses.

I am trying to do the following:

test_model = Model(
        "_id": fields.String,
        "name": fields.String,
        "location": fields.String,
        "ip": fields.String,

success_model = Model(
    {"data": fields.List(

Without throwing any exception the data in the response is null

[{"status": "success", "data": null}']
Can somebody give me an advice?

Patrick Ray. Huang
hello all, I am new to restx, wondering when creating a model that has a one to many relationship, (e.g, order and order_items), how do i specify the field in the order model?
Tiago Tavares
Could enable the creation of models without automatically documenting, please?
It would be great to use @api.marshal_with(model) with multiple models, but not letting all of them be documented in swagger