Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

RuntimeError: You cannot define both `content_type` and `headers[Content-Type]`.

Đang mở
#765 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
testing

Hướng nghiên cứu

Bắt đầu trong responses._recorder, quanh recorder.dump_to_file và đường dẫn replay sử dụng RequestsMock._add_from_file, sau đó tái hiện bằng YAML được nêu trong issue. Theo dõi cách content_type và headers[Content-Type] đã ghi được tải; hoàn thành khi response được replay mà không xảy ra RuntimeError và vẫn giữ nguyên metadata của response đã ghi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Describe the bug

In my pytests, I record responses with the fixture

import hashlib

import pytest
import responses
from responses._recorder import recorder


@pytest.fixture
def mock_requests(request):
    fname = hashlib.sha256(request.node.nodeid.encode()).hexdigest()[:16]
    yml_path = request.path.parent / "responses" / f"{fname}.yml"

    if request.config.getoption("--update-responses"):
        yml_path.parent.mkdir(exist_ok=True)
        if yml_path.exists():
            yml_path.unlink()
        with recorder as r:
            yield r
            # The register might in fact be empty, but never mind
            recorder.dump_to_file(
                file_path=yml_path,
                registered=r.get_registry().registered,
            )
    else:
        assert yml_path.exists()
        with responses.RequestsMock() as r:
            r._add_from_file(file_path=yml_path)  # noqa: SLF001
            yield r

This usually works fine, but I sometimes get responses like

responses:
  - response:
      auto_calculate_content_length: false
      body:
        '{"status":"ok","message-type":"work","message-version":"1.0.0","message":{"indexed":{"date-parts":[[2025,4,5]],"date-time":"2025-04-05T15:23:57Z","timestamp":1743866637814},"edition-number":"1","reference-count":0,"publisher":"Wiley","isbn-type":[{"value":"9780471967583","type":"print"},{"value":"9780470868270","type":"electronic"}],"license":[{"start":{"date-parts":[[2003,6,6]],"date-time":"2003-06-06T00:00:00Z","timestamp":1054857600000},"content-version":"tdm","delay-in-days":0,"URL":"http:\/\/doi.wiley.com\/10.1002\/tdm_license_1.1"}],"content-domain":{"domain":[],"crossmark-restriction":false},"short-container-title":[],"published-print":{"date-parts":[[2003,6,6]]},"DOI":"10.1002\/0470868279","type":"monograph","created":{"date-parts":[[2005,1,28]],"date-time":"2005-01-28T19:00:13Z","timestamp":1106938813000},"source":"Crossref","is-referenced-by-count":547,"title":["Numerical
        Methods for Ordinary Differential Equations"],"prefix":"10.1002","author":[{"given":"J.C.","family":"Butcher","sequence":"first","affiliation":[]}],"member":"311","published-online":{"date-parts":[[2005,1,28]]},"container-title":[],"original-title":[],"language":"en","link":[{"URL":"https:\/\/onlinelibrary.wiley.com\/doi\/pdf\/10.1002\/0470868279","content-type":"application\/pdf","content-version":"vor","intended-application":"text-mining"},{"URL":"https:\/\/onlinelibrary.wiley.com\/doi\/pdf\/10.1002\/0470868279","content-type":"unspecified","content-version":"vor","intended-application":"similarity-checking"}],"deposited":{"date-parts":[[2023,8,20]],"date-time":"2023-08-20T18:49:32Z","timestamp":1692557372000},"score":1,"resource":{"primary":{"URL":"https:\/\/onlinelibrary.wiley.com\/doi\/book\/10.1002\/0470868279"}},"subtitle":[],"short-title":[],"issued":{"date-parts":[[2003,6,6]]},"ISBN":["9780471967583","9780470868270"],"references-count":0,"alternative-id":["10.1002\/0470868279"],"URL":"https:\/\/doi.org\/10.1002\/0470868279","relation":{},"subject":[],"published":{"date-parts":[[2003,6,6]]}}}'
      content_type: text/plain
      headers:
        access-control-allow-headers:
          X-Requested-With, Accept, Accept-Encoding, Accept-Charset,
          Accept-Language, Accept-Ranges, Cache-Control
        access-control-allow-origin: "*"
        access-control-expose-headers: Link
        connection: close
        content-encoding: gzip
        content-length: "871"
        content-type: application/json
        date: Tue, 08 Apr 2025 12:35:37 GMT
        permissions-policy: interest-cohort=()
        server: Jetty(9.4.40.v20210413)
        set-cookie:
          AWSALB=q+h9ABpvNWjFxcvQm3RXWezoK9cDzdpQ744+UzK8XrW4K/QpNE49u2geK33JiVSCotsja9oBo0d4Eq9EWYfPAlFVfltotJjVtr9Ws/ytgxvmlycVk1/rSWOT+aiz;
          Expires=Tue, 15 Apr 2025 12:35:37 GMT; Path=/, AWSALBCORS=q+h9ABpvNWjFxcvQm3RXWezoK9cDzdpQ744+UzK8XrW4K/QpNE49u2geK33JiVSCotsja9oBo0d4Eq9EWYfPAlFVfltotJjVtr9Ws/ytgxvmlycVk1/rSWOT+aiz;
          Expires=Tue, 15 Apr 2025 12:35:37 GMT; Path=/; SameSite=None
        vary: Accept-Encoding
        x-api-pool: polite
        x-rate-limit-interval: 1s
        x-rate-limit-limit: "50"
        x-ratelimit-interval: 1s
        x-ratelimit-limit: "50"
      method: GET
      status: 200
      url: https://api.crossref.org/works/10.1002%2F0470868279

When replaying, this leads to the error

E               RuntimeError: You cannot define both `content_type` and `headers[Content-Type]`. Using the `content_type` kwarg is recommended.

There is no content_type specification in my own code.

Additional context

No response

Version of responses

0.25.7

Steps to Reproduce

See above.

Expected Result

See above.

Actual Result

See above.

Ngôn ngữ chính
Python
Star
4.3k
Fork
389
Merge trung bình
1 ngày 17 giờ
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của getsentry/responses

Tất cả issue của getsentry/responses

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.