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

HTTP ingress: optional query params don't work #1686

Closed
tomdaffurn opened this issue Jun 6, 2024 · 0 comments · Fixed by #1694
Closed

HTTP ingress: optional query params don't work #1686

tomdaffurn opened this issue Jun 6, 2024 · 0 comments · Fixed by #1694
Assignees
Labels
bug Something isn't working

Comments

@tomdaffurn
Copy link
Contributor

Optional body parameters can be done with ftl.Option. I assume it can be done with query params too:

type GetRequest struct {
	UserID string             `json:"userId"`
	PostID string             `json:"postId"`
	Tag    ftl.Option[string] `json:"tag"`
}

// Example:       curl -i http://localhost:8891/http/users/123/posts?postId=456&tag=foo
//
//ftl:ingress http GET /http/users/{userId}/posts
func Get(ctx context.Context, req builtin.HttpRequest[GetRequest]) (builtin.HttpResponse[GetResponse, ErrorResponse], error) {
	if req.Body.UserID == "000" {
		return builtin.HttpResponse[GetResponse, ErrorResponse]{
			Error: ftl.Some(ErrorResponse{
				Error: "User not found",
			}),
		}, nil
	}
	tag := req.Body.Tag.Default("No value")
	return builtin.HttpResponse[GetResponse, ErrorResponse]{
		Headers: map[string][]string{"Get": {"Header from FTL"}},
		Body: ftl.Some(GetResponse{
			Message: fmt.Sprintf("Got userId %s, postId %s, tag %s", req.Body.UserID, req.Body.PostID, tag),
			Nested:  Nested{GoodStuff: "Nested Good Stuff"},
		}),
	}, nil
}

But it does't:

 % curl "http://localhost:8891/http/users/1/posts?postId=2&tag=foo" 
{"msg":"Got userId 1, postId 2, tag No value","nested":{"good_stuff":"Nested Good Stuff"}}%    
@github-actions github-actions bot added the triage Issue needs triaging label Jun 6, 2024
@tomdaffurn tomdaffurn added bug Something isn't working and removed triage Issue needs triaging labels Jun 6, 2024
@alecthomas alecthomas added the triage Issue needs triaging label Jun 6, 2024
@ftl-robot ftl-robot mentioned this issue Jun 6, 2024
@wesbillman wesbillman self-assigned this Jun 6, 2024
@github-actions github-actions bot removed the triage Issue needs triaging label Jun 6, 2024
wesbillman added a commit that referenced this issue Jun 7, 2024
Fixes #1686

Here's the test I used:

```go
type GetRequest struct {
	UserID string             `json:"userId"`
	PostID string             `json:"postId"`
	Tag    ftl.Option[string] `json:"tag"`
}

type GetResponse struct {
}

// Example:       curl -i http://localhost:8891/http/users/123/posts?postId=456&tag=foo
//
//ftl:ingress http GET /http/users/{userId}/posts
func Get(ctx context.Context, req builtin.HttpRequest[GetRequest]) (builtin.HttpResponse[GetResponse, string], error) {
	tag := req.Body.Tag.Default("No value")
	ftl.LoggerFromContext(ctx).Infof("Received request with tag: %s", tag)
	return builtin.HttpResponse[GetResponse, string]{
		Headers: map[string][]string{"Get": {"Header from FTL"}},
		Body:    ftl.Some(GetResponse{}),
	}, nil
}
```

```bash
curl -i http://localhost:8891/http/users/123/posts\?postId\=456\&tag\=foo

info:echo: Received request with tag: foo
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants