Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom query parameter #3

Closed
MortalFlesh opened this issue Jun 1, 2020 · 3 comments
Closed

Custom query parameter #3

MortalFlesh opened this issue Jun 1, 2020 · 3 comments

Comments

@MortalFlesh
Copy link

Hello,

I'm trying to add a custom query parameter for POST method.

Reason/Use-case

I have a multi-tenant api, and the caller has to provide a name of his tenant - so api would know, which part of the database it should use.

Problem

I've found this custom-query-parameters in docs and example here

let! someParam = parser.GetRequired(Query.Bool("someQueryParam"))

I've tried

let post =
        define.Operation
            .Post(fun ctx parser ->
                parser.ForAsyncRes(
                    asyncResult {
                        let! spot = parser.GetOptional(Query.ParsedOpt("spot", Some)) |> Hopac.Job.toAsync
                        printfn "Spot: %A" spot
                        // ...

and also Query.String("spot")

I also tried it in creating jsonApiContext to make it "globally" available, by parsing HttpContext directly.

But I get this error every time:

{
  "errors": [
    {
      "id": "4bb6ea2bdb1d4e7cba15d9273bd2ebb2",
      "status": "400",
      "title": "Invalid query parameter name",
      "detail": "'spot' is not an allowed query parameter name according to the JSON:API specification",
      "source": {
        "parameter": "spot"
      }
    }
  ]
}

Should I register my custom query parameters somehow first? Is it against json-api specification?

Thanks in advance!

@cmeeren
Copy link
Owner

cmeeren commented Jun 1, 2020

According to the JSON:API spec, query parameter names must contain at least one non-lowercase character. (The rules are a bit more complex, but that's the simple version.) For example, spotId or Spot would be valid, but spot is not, because all lowercase-only query parameter names are reserved by the JSON:API spec for future use.

Here's the spec.

@cmeeren
Copy link
Owner

cmeeren commented Jun 1, 2020

By the way, in your case, if you use the string value directly, Query.String("myParam") would be the simplest alternative.

@MortalFlesh
Copy link
Author

Hello,

I'll use Spot then. Thanks for explanation.


By the way, in your case, if you use the string value directly, Query.String("myParam") would be the simplest alternative.

Yea I though so, I just tried different ways to test the scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants