pact-foundation / pact-provider-verifier

Cross-platform, generic language, Pact provider verification tool
http://pact.io
MIT License
32 stars 24 forks source link

JSON malformed on GET requests with body #105

Open lluis-jt opened 1 year ago

lluis-jt commented 1 year ago

Seems there is a problem with sending a wrong formatted JSON body on a GET request with a JSON body.

I am executing the next command: bundle exec pact-provider-verifier /hq-placements-client-hq-placements.json --provider hq-placements --provider-base-url 'http://localhost:3000/'

The body in the contract is this one:

"body": {
  "sort_field": "not permitted value"
}

And I can see that the provider is receiving this body: "{\"sort_field\":\"not permitted value\"}"=>nil

But in case I use the query instead of body in the contract JSON file: "query": "sort_field=invalid", the request works properly.

Also, mention that I can do the same request using Postman or ‘curl’ and it works properly:

curl --location --request GET 'localhost:3000/placements' \
--header 'Content-Type: application/json' \
--data '{
          "sort_field": "not permitted value"
}'

Did some debugging and I can see that when the query method is a GET (catching it with Wireshark), the body is translated into query params, but with what it seems with white spaces (what is formerly accepted, since the body on a GET request is not the ‘correct’ way to handle the request). As fast as I change it to other methods, the body appears in the correct way.

So it parses the body into %7B%22sort_field%22%3A%22invalid%22%7D when it should be either a JSON body or a query parameter like sort_field=invalid

Screenshot 2023-05-16 at 16 59 23

I am using a contract with this specification: "pactSpecification": { "version": "2.0.0" } For the gem: pact-provider-verifier (1.36.1)

YOU54F commented 1 year ago

GET requests shouldn't have bodies

YOU54F commented 1 year ago

Well you can send one, but it should be ignored by clients. Whether Pact should do they same, is open to interpretation