okigan / awscurl

curl-like access to AWS resources with AWS Signature Version 4 request signing.
MIT License
737 stars 91 forks source link

Using `-d` arg doesn't automatically make the request method POST #144

Open gopuneet opened 1 year ago

gopuneet commented 1 year ago

Using -d arg doesn't automatically make the request method POST and sends a GET request which is unexpected behaviour since curl automatically makes the method POST when using -d arg (Refer curl -d manpage & curl tutorial example for post).

Reproducible Example

Here, we are using Neptune Statistics API which uses the same endpoint for getting status, disabling/enabling autoCompute, manually triggering stats generation etc.

  1. Statistics status (GET request)
    % awscurl -k --service neptune-db --profile test https://${ENDPOINT}:8182/pg/statistics
    {
    "status" : "200 OK",
    "payload" : {
        "autoCompute" : false,
        "active" : true,
        "statisticsId" : 1657533072688,
        "date" : "2022-07-11T09:51Z",
        "summary" : {
            "signatureCount" : 3,
            "instanceCount" : 3,
            "predicateCount" : 5
        }
    }
    }
  2. Enable AutoCompute (-d is specified so it should automatically become a POST request but a GET request is sent which can be verified by adding -v)
    % awscurl -k --service neptune-db --profile test https://${ENDPOINT}:8182/pg/statistics \
    -d '{ "mode" : "enableAutoCompute" }'
    {
    "status" : "200 OK",
    "payload" : {
        "autoCompute" : false,
        "active" : true,
        "statisticsId" : 1657533072688,
        "date" : "2022-07-11T09:51Z",
        "summary" : {
            "signatureCount" : 3,
            "instanceCount" : 3,
            "predicateCount" : 5
        }
    }
    }
  3. Enable AutoCompute (explicitely specifying -X POST)
    % awscurl -k --service neptune-db --profile test https://${ENDPOINT}:8182/pg/statistics \
    -d '{ "mode" : "enableAutoCompute" }' -X POST
    {
    "status" : "200 OK"
    }
gopuneet commented 1 year ago

https://github.com/okigan/awscurl/blob/b4d48077acd138e67fd9b88deb22524525249a0d/awscurl/awscurl.py#L467-L473

After going through the code, I think the issue can be fixed near the above code block by adding:

data = args.data

if data is not None and args.request != 'POST':
    args.request = 'POST'

# rest same from Line 469
okigan commented 1 year ago

Cool, thanks for the find, keeping consistency with curl make it very convenient - please send PR