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

failed to build request: http::Error(InvalidUri(InvalidFormat)) when testing from API Gateway in the AWS Console #368

Closed
ionionascu opened this issue Nov 19, 2021 · 6 comments

Comments

@ionionascu
Copy link

When testing from the API Gateway's test method without explicitely providing the Host http header, I get the error failed to build request: http::Error(InvalidUri(InvalidFormat)), which I didn't use to get (maybe the test request has been changed on AWS side).

This test reproduces the error on the current master branch:

#[test]
fn test() {
    const PAYLOAD: &str = r#"{
        "resource": "/test",
        "path": "/test",
        "httpMethod": "POST",
        "headers": {
            "test": "test"
        },
        "multiValueHeaders": {
            "test": [
                "test"
            ]
        },
        "queryStringParameters": {
            "test": "test"
        },
        "multiValueQueryStringParameters": {
            "test": [
                "test"
            ]
        },
        "pathParameters": null,
        "stageVariables": null,
        "requestContext": {
            "resourceId": "resourceId",
            "resourcePath": "/test",
            "operationName": "lambda",
            "httpMethod": "POST",
            "requestTime": "19/Nov/2021:12:19:02 +0000",
            "path": "/test",
            "accountId": "123",
            "protocol": "HTTP/1.1",
            "stage": "test-invoke-stage",
            "domainPrefix": "testPrefix",
            "requestTimeEpoch": 1637324342768,
            "requestId": "123",
            "identity": {
                "cognitoIdentityPoolId": null,
                "cognitoIdentityId": null,
                "apiKey": "test-invoke-api-key",
                "principalOrgId": null,
                "cognitoAuthenticationType": null,
                "userArn": "arn",
                "apiKeyId": "test-invoke-api-key-id",
                "userAgent": "aws-internal/3 aws-sdk-java/1.12.76 Linux/5.4.147-83.259.amzn2int.x86_64 OpenJDK_64-Bit_Server_VM/25.302-b08 java/1.8.0_302 vendor/Oracle_Corporation cfg/retry-mode/standard",
                "accountId": "123",
                "caller": "caller",
                "sourceIp": "test-invoke-source-ip",
                "accessKey": "accessKey",
                "cognitoAuthenticationProvider": null,
                "user": "user"
            },
            "domainName": "testPrefix.testDomainName",
            "apiId": "apiId"
        },
        "body": null,
        "isBase64Encoded": false
    }"#;
    
    let val: lambda_http::request::LambdaRequest = serde_json::from_str(PAYLOAD).unwrap();
    let _http_request: http::Request<Body> = http::Request::from(val);
}

In my case, the error being generated here https://github.com/awslabs/aws-lambda-rust-runtime/blob/master/lambda-http/src/request.rs#L472

@nmoutschen
Copy link
Contributor

nmoutschen commented Nov 19, 2021

Hey @ionionascu! Thanks for creating a new issue for this.

Looks like the issue comes from the request body generated by the Lambda console test event, as it's returning a null value instead of an empty string.

As far as I'm aware, API Gateway doesn't send null values for bodies, but double-checking internally first before making a PR to support null values for this field.

@ionionascu
Copy link
Author

Hi @nmoutschen. The sample request I put in the test comes from data received by my lambda went I did a test from the API Gateway's Test Method feature in the AWS Console. I just changed the values of some sensitive attributes. If, in the Test Method page I add the header Host with a value that is a valid hostname, then I do not get the error.

Separate from this, to be fair, unless there is a public AWS page that explicitely says that an attribute inside the request JSON follows certain rules, then I think all options should be supported (e.g. null should also be supported for the headers). I may be wrong, of course, but I do not remember seeing such a page.

@nmoutschen
Copy link
Contributor

Thanks for the explanations! I just realized that I misread your original message. 🤦 My apologies for this.

I'll look into it.

@nmoutschen
Copy link
Contributor

Hey @ionionascu !

I was able to pinpoint the issue to how the crate was trying to generate the Uri. If the host header was absent, it would try to create an Uri from a value looking like https:///test (with three slashes), which is not a valid URL. I've created a PR which would change that behavior to /test instead when the Host header is absent.

See #369

@nmoutschen
Copy link
Contributor

Hey @ionionascu !

The PR is merged now. Could you check if you're still facing that issue against the git repository directly?

@ionionascu
Copy link
Author

Thank you @nmoutschen. I have tested with the current master branch and I can no longer reproduce this issue, so I will close it.

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