question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing array in query via axios

See original GitHub issue

Hi,

I’m using axios to send a GET request to my flask-smorest endpoint. I know there’s no real agreement on this spec-wise, but the request’s querystring contains an array, and axios sends it in this format:

http://127.0.0.1:5000/api/v1/items/?types[]=cd&types[]=dvd

I’ve defined the schema used for reading the arguments as

class FilterSchema(ma.Schema):
    ...
    types = ma.fields.List(ma.fields.String(), missing=[])

Yet when I try to read the data received in my endpoint, types is empty:

@items_blp.route('/')
class ItemCollection(MethodView):
    @items_blp.arguments(FilterSchema(unknown=ma.EXCLUDE), location="query")
    @listings_blp.response(ItemSchema(many=True))
    def get(self, payload):
        # payload['types'] is empty...
        if payload['types']:
            qs = Item.objects.filter(item_type__in=payload['types'])
        return qs

Is this a missing feature in flask-smorest or should I ask on SO whether I should use another way to pass data?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ThiefMastercommented, Nov 18, 2020

Just FYI, you can configure axios to not do this [] nonsense that started in PHP when they thought it’d be even remotely a good idea to let the client decide whether unstructured form data should become an array or not…

Just pass paramsSerializer: params => qs.stringify(params, {arrayFormat: 'repeat'}) to your axios calls (or better: use axios.create() to create an instance where you pass this as a setting and use that instance everywhere)

1reaction
lafrechcommented, Aug 23, 2021

There’s two issues

  • 1/ Parsing this query string format correctly.
  • 2/ Documenting it correctly

1/ is a webargs issue, so I’ll transfer to webargs. You’ll have to write a custom parser (see https://webargs.readthedocs.io/en/latest/advanced.html#custom-parsers).

2/ is an apispec issue. I’m not sure there’s a way to write a spec that will display correctly in a frontend such as ReDoc or Swagger-UI, so you might end up just adding a generic comment at the beginning of the spec.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to correctly use axios params with arrays - Stack Overflow
I got using "paramSerializer" a bit confuse. Before looking for the "right way" to use axios with array querystring on Google, ...
Read more >
Providing an array as a param isn't working as expected. #604
I want to put in the following get request using axios config.params object: https://api.nal.usda.gov/ndb/nutrients/?format=json&api_key=...
Read more >
Sending Array Of Objects in GET request (axios) - Reddit
For filter values i have to send array of objects in parameters. Now I know when we want to send complex params in...
Read more >
GET Request Query Params with Axios - Mastering JS
The easiest way to make a GET request with Axios is the axios.get() function. The 2nd parameter to axios.get() is the Axios options:...
Read more >
Request Config | Axios Docs
It can be convenient to set `baseURL` for an instance of axios to pass relative ... 'PATCH' and 'DELETE' // The last function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found