Byron / google-apis-rs

A binding and CLI generator for all Google APIs
http://byron.github.io/google-apis-rs
Other
983 stars 132 forks source link

`Content-Length` header missing when no body was provided in post request, causing request failure #476

Closed Dreaming-Codes closed 4 months ago

Dreaming-Codes commented 4 months ago

Hi, the latest schema update introduced a new method google_play_console_hub..purchases().products_consume which is a fundamental method to consume a bought product to allow the user to buy it again but that method always gives me the following error:

Http status indicates failure: Response { status: 411, version: HTTP/1.0, headers: {"content-type": "text/html; charset=UTF-8", "referrer-policy": "no-referrer", "content-length": "1564", "date": "Sat, 09 Mar 2024 11:16:57 GMT"}, body: Body(Full(b"<!DOCTYPE html>\n\n \n <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n Error 411 (Length Required)!!1\n \n \n

411. That\xe2\x80\x99s an error.\n

POST requests require a Content-length header. That\xe2\x80\x99s all we know.\n")) }

Dreaming-Codes commented 4 months ago

Comparing it with another post request in the generated code it seams like this part is missing products_acknowledge: https://github.com/Byron/google-apis-rs/blob/ef8dc60e716dba545453f70caca23ac97427fe6c/gen/androidpublisher3/src/api.rs#L36398-L36408

products_consume: https://github.com/Byron/google-apis-rs/blob/ef8dc60e716dba545453f70caca23ac97427fe6c/gen/androidpublisher3/src/api.rs#L36696

Byron commented 4 months ago

There cannot be support for any of the generated APIs, they are provided by Google. This repository is about the generator itself, and no other support can be provided here. Thanks for your understanding.

Dreaming-Codes commented 4 months ago

@Byron Please read what I added above as a comment

Byron commented 4 months ago

Comparing it with another post request in the generated code it seams like this part is missing products_acknowledge: https://github.com/Byron/google-apis-rs/blob/ef8dc60e716dba545453f70caca23ac97427fe6c/gen/androidpublisher3/src/api.rs#L36398-L36408

https://github.com/Byron/google-apis-rs/blob/ef8dc60e716dba545453f70caca23ac97427fe6c/gen/androidpublisher3/src/api.rs#L36696

You can try different versions of the generated code, there is 5.0.3 and 5.0.4, which uses different versions of the API.

Dreaming-Codes commented 4 months ago

@Byron nope products_consume is a new method

Dreaming-Codes commented 4 months ago

@Byron I think that this is a bug in the generator since it's skipping a required post header

Byron commented 4 months ago

Please feel free to submit a PR with a fix. If that allegedly missing header can't be inferred from the API description, maybe the API can be changed to allow the user to inject their own headers.

Dreaming-Codes commented 4 months ago

Please feel free to submit a PR with a fix. If that allegedly missing header can't be inferred from the API description, maybe the API can be changed to allow the user to inject their own headers.

@Byron The header missing is a required http header not a custom one, it's Content-length I mean a post request should always have that even if not specified in the API description

Dreaming-Codes commented 4 months ago

I would fix that, but I really don't know how Mako works.

Byron commented 4 months ago

You can probably look at the generated code to see under which circumstances it does or doesn't add that header.

Dreaming-Codes commented 4 months ago

@Byron the only difference that I notice from other post requests is the missing request key acknowledge: https://github.com/Byron/google-apis-rs/blob/ef8dc60e716dba545453f70caca23ac97427fe6c/etc/api/androidpublisher/v3/androidpublisher-api.json#L3888-L3890

consume: https://github.com/Byron/google-apis-rs/blob/ef8dc60e716dba545453f70caca23ac97427fe6c/etc/api/androidpublisher/v3/androidpublisher-api.json#L3895-L3929

Dreaming-Codes commented 4 months ago

I think that I found the problem. basically when there's no body the generator does not add the Content-length header

https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.products/consume#request-body

Byron commented 4 months ago

Thanks for the research. That sounds like something fixable, even though a fix will have to be contributed.

Dreaming-Codes commented 4 months ago

I think that the error in the generation happens here https://github.com/Byron/google-apis-rs/blob/ef8dc60e716dba545453f70caca23ac97427fe6c/src/generator/templates/api/lib/mbuild.mako#L752-L763

Dreaming-Codes commented 4 months ago

It may be enough to add .header(CONTENT_LENGTH, 0_u64) between lines 761 and 762.

Byron commented 4 months ago

It appears like it, and is a strange omission as well. You can probably try it locally, and even if not, CI will be able to validate the generated code is still correct.

Dreaming-Codes commented 4 months ago

@Byron I'm trying to get nix on my system to work so I can get python 3.8

Byron commented 4 months ago

Getting a local dev setup is harder than it should be due to needing python, thus I recommend creating a PR with this seemingly trivial change (blindly, if you will), and see if that works.

For local testing, the change can be added by hand in the generated code, and used with your application via [patch."crates-io"] section in the Cargo manifest.

Dreaming-Codes commented 4 months ago

Getting a local dev setup is harder than it should be due to needing python, thus I recommend creating a PR with this seemingly trivial change (blindly, if you will), and see if that works.

@Byron I've create a pull request with a .devcontainer for easier development

477