Skip to content

How should I invoke HTTP examples locally? #610

Closed

Description

@timClicks

I'm slightly confused about how to invoke the HTTP examples on the command line via cargo lambda invoke.

For example, the http-axum example provides a few HTTP accessible paths, but I'm unsure how to reach them. http-basic-lambda is even more frustrating, because it doesn't accept any arguments, yet the invoke sub-command requires that users specific data to sent.

What data should I be providing? How do I send GET vs POST requests? Is it possible to simply use curl?

Activity

  1. changed the title [-]How should I invoke examples with multiple HTTP routes?[/-] [+]How should I invoke HTTP examples locally?[/+] on Mar 2, 2023
  2. bnusunny commented on Mar 2, 2023

    @bnusunny
    Collaborator

    Yes, you can use curl. It is in the README now. https://github.com/awslabs/aws-lambda-rust-runtime#cargo-lambda

    curl -v -X POST \
      'http://127.0.0.1:9001/lambda-url/<lambda-function-name>' \
      -H 'content-type: application/json' \
      -d '{ "command": "hi" }'
    
  3. timClicks commented on Mar 2, 2023

    @timClicks
    ContributorAuthor

    Thanks for your answer @bnusunny.

    Two follow-on questions. How is <lambda-function-name> defined? I assume that it's the name of the bin target.

    Is it possible to elaborate on what data should I be providing to the functions. How does the JSON payload map to something that the function handler can interpret?

  4. greenwoodcm commented on Mar 2, 2023

    @greenwoodcm
    Contributor

    if you use the */lambda-url/<lambda-function-name>/* style then you can just curl as you would expect. cargo lambda is going to do the translation of your curl request into the JSON payload, and then the Rust Runtime is going to do the de-translation of that JSON payload back into an HTTP request.

    curl -X GET -H "Content-Type: application/json" http://localhost:9001/lambda-url/http-warp/foo/bar
    
  5. winstxnhdw commented on Mar 3, 2023

    @winstxnhdw
    Contributor

    Hey Tim! I was in the exact predicament as you a few weeks back.

    Two follow-on questions. How is defined? I assume that it's the name of the bin target.

    Yes, lambda-function-name is the name of the package defined in Cargo.toml. You can get the specifics from the cargo-lambda invoke docs.

    Is it possible to elaborate on what data should I be providing to the functions. How does the JSON payload map to something that the function handler can interpret?

    If you are using the payload() method, you can view the implementation here. To elaborate further, if you expect the body of your request event to be of the following.

    curl -v -X POST \
      'http://127.0.0.1:9001/lambda-url/<lambda-function-name>' \
      -H 'content-type: application/json' \
      -d \
       "{
           \"words\": [\"hello\", \"world\"],
           \"number\": 5
        }"

    You should define a struct like so.

    #[derive(Deserialize)]
    struct RequestEvent {
        words: Vec<String>,
        number: i32
    }
    
    async fn handler(event: Request) -> Result<Response<Body>, Error> {
        let payload = match event.payload::<RequestEvent>() {
            Ok(Some(payload)) => payload,
            Ok(None) => {
                return Ok(Response::builder()
                    .status(400)
                    .body("No body found".into())
                    .map_err(Box::new)?)
            }
            Err(err) => {
                return Ok(Response::builder()
                    .status(400)
                    .body(format!("Error parsing body: {}", err).into())
                    .map_err(Box::new)?)
            }
        };
    }

    Ideally, the entire docs should be completely rewritten. To prevent further confusion, anything related to cargo-lambda should not be mentioned anywhere else apart from its dedicated section, but this is what we have to work with for now :)

  6. locked and limited conversation to collaborators on Apr 10, 2023
  7. converted this issue into a discussion #632 on Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions