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

Rust SDK: Query params seem to be missing from http::Request #313

Closed
technosophos opened this issue Apr 5, 2022 · 9 comments
Closed

Rust SDK: Query params seem to be missing from http::Request #313

technosophos opened this issue Apr 5, 2022 · 9 comments
Labels
bug Something isn't working sdk

Comments

@technosophos
Copy link
Contributor

If I send a request like http://localhost:3000/index?foo=bar&baz=sprocket, I would expect the query parameters to be available on the URI object.

However, if I do req.uri(), the URL does not have query parameters attached. This is easy to replicate by just printing the uri(). However, since currently every request seems to be automatically printed to STDOUT, you can see this in the log without doing anything special.

Command: curl localhost:3000/tally\?ulid=1234\&foo=bar (Note that backslashes are just for shell escaping)

Content of the STDOUT log:

Request { method: GET, uri: /tally, version: HTTP/1.1, headers: {"host": "localhost:3000", "user-agent": "curl/7.77.0", "accept": "*/*", "spin-path-info": "", "spin-full-url": "http://localhost:3000/tally", "spin-matched-route": "/tally", "spin-base-path": "/", "spin-raw-component-route": "/tally", "spin-component-route": "/tally"}, body: Some(b"") }
@technosophos technosophos added the bug Something isn't working label Apr 5, 2022
@technosophos technosophos changed the title Query params seem to be missing from http::Request Rust SDK: Query params seem to be missing from http::Request Apr 5, 2022
@itowlson
Copy link
Contributor

itowlson commented Apr 5, 2022

This has been a source of vigorous discussion and (to me at least) total confusion.

The uri field in the Request object is NOT the full URI. As I understand it, it is the path relative to the application root.

My advice to young developers such as yourself is to avoid uri altogether, and use the various headers which are much less ambiguous about what they mean. (See https://spin.fermyon.dev/http-trigger and scroll down to "The default headers set in Spin HTTP components.)

@radu-matei Do we document the semantics of the uri field? I thought we planned to but can't see it on the HTTP trigger page.

@itowlson
Copy link
Contributor

itowlson commented Apr 5, 2022

The discussion is here, and if the final post is still current, further demonstrates my confusion - apparently the uri is the absolute path not the relative path.

#72

@itowlson
Copy link
Contributor

itowlson commented Apr 5, 2022

Hang on, the Request object passed to the SDK handler method doesn't seem to match the "HTTP objects" described on the https://spin.fermyon.dev/http-trigger page. So maybe I am completely misleading you.

@itowlson
Copy link
Contributor

itowlson commented Apr 5, 2022

I think the SDK macro is generating a Rust http::Request object from the WIT uri as if that was a full URL.

@fibonacci1729 I think at

let mut http_req = http::Request::builder()
you need to be reconstructing the URI using the params member of the source object, or using the spin-full-url header.

@technosophos
Copy link
Contributor Author

I am not sure what you are suggesting, but inside of a component, the only thing I get is the http::Request, and the canonical way to get the params off of the Request is to use the uri().query() method... and that is empty every time.

@itowlson
Copy link
Contributor

itowlson commented Apr 5, 2022

Yeah I think the Spin site (and the issue I linked to) covers the "raw" request type and I hadn't clocked that the macro SDK converts it to the "native" Request type (but that the macro contains a bug where it doesn't restore the query). Sorry.

@technosophos
Copy link
Contributor Author

I have been using these two things as reference:

If you wanna direct me to something else, I'll give it a try.

@itowlson
Copy link
Contributor

itowlson commented Apr 5, 2022

No, it's just a bug in the SDK, hence tagging Brian with the location of the bug. Sorry for confusing the issue.

@radu-matei
Copy link
Member

radu-matei commented Apr 5, 2022

the canonical way to get the params off of the Request is to use the uri().query() method... and that is empty every time.

This is how the parameters should be obtained when using the Rust SDK, and this is definitely a bug.
(including the fact that the full URL headers should also include the query parameters)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working sdk
Projects
None yet
Development

No branches or pull requests

4 participants