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

[PYTHON] Error when receiving .zip file octet-stream response from API endpoint #12311

Open
NA-Dev opened this issue Dec 22, 2023 · 1 comment

Comments

@NA-Dev
Copy link

NA-Dev commented Dec 22, 2023

Description

I generated a client to consume an API. One of the endpoints returns an octet-stream of a zipped file with .zip extension. When receiving this file, I get the following error trace. I get that this is to do with encoding, but I have no idea how to make the codegen generate code that works. When I try chardet it says {'encoding': None, 'confidence': 0.0, 'language': None}. When I fetch the report in Postman, I can save the raw response as a zip file and extract a csv file with no issues.

  File "/home/user/dev/my_sdk/api_client.py", line 328, in call_api
    return self.__call_api(resource_path, method,
  File "/home/user/dev/my_sdk/api_client.py", line 156, in __call_api
    response_data = self.request(
  File "/home/user/dev/my_sdk/api_client.py", line 350, in request
    return self.rest_client.GET(url,
  File "/home/user/dev/my_sdk/rest.py", line 257, in GET
    return self.request("GET", url,
  File "/home/user/dev/my_sdk/rest.py", line 226, in request
    r.data = r.data.decode('utf8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaa in position 10: invalid start byte
Swagger-codegen version

3.0.30

Swagger declaration file content or url
openapi: "3.0.0"
info:
  title: My API 
  description: >-
    Contact Support:
     Name: No Contact
     Email: email@example.com
  version: 1.0.0
servers:
  - url: '{server}'
    variables:
      server:
        default: https://myserver.com
components:
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic    
security:
  - basicAuth: []
paths:
  /fetchZipFile:
    get:
      operationId: getZipFile
      summary: getZipFile
      parameters:
        - name: Accept
          in: header
          schema:
            type: string
          example: application/octet-stream
        - name: Content-Type
          in: header
          schema:
            type: string
          example: application/octet-stream
        - name: date
          in: query
          schema:
            type: integer
          example: '09122023'
      responses:
        '200':
          description: OK
          headers:
            Accept-Ranges:
              schema:
                type: string
                example: bytes
            Content-Disposition:
              schema:
                type: number
                example: >-
                  attachment;filename=Report_2023-09-14.zip
            Content-Type:
              schema:
                type: string
                example: application/octet-stream
            Content-Length:
              schema:
                type: integer
                example: '189'
            Cache-Control:
              schema:
                type: string
                example: max-age=0, no-cache, no-store
            Date:
              schema:
                type: string
                example: Wed, 09 Aug 2023 18:43:13 GMT
            Connection:
              schema:
                type: string
                example: keep-alive
          content:
            application/octet-stream:
              schema:
                type: file
Command line used for generation

java -jar swagger-codegen-cli.jar generate
-i openapi-spec.yaml
-l python
-o generated_code
-c config.json

Steps to reproduce

Use the generated client to request the zip file, and you get the error

Related issues/PRs
Suggest a fix/enhancement
@NA-Dev NA-Dev changed the title [JAVA] Error when receiving .zip file octet-stream response from API endpoint [PYTHON] Error when receiving .zip file octet-stream response from API endpoint Dec 22, 2023
@NA-Dev
Copy link
Author

NA-Dev commented Dec 22, 2023

I think this is a file that needs to be saved without decoding. Can I generate a client that does that, or do I need to set _preload_content to False, and handle it all manually?

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

1 participant