question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Headers aren't added to response

See original GitHub issue

Ahoi,

I’m trying to add headers to a response but can’t figure out why it doesn’t work. I tried these ways:

url = 'https://example.com/?cursor='
resp_body = [{'foo': 'bar'}, ]

responses.add(
    responses.Response(
        method='GET',
        url=url,
        json=resp_body,
        status=200,
        headers={
            'Link': f'<{ url }>; rel="first", <{ url }next_cursor>; rel="next"'
        },
        content_type='application/json'
    )
)
responses.add(
    responses.GET,
    url=f'{ url }next_cursor',
    json=resp_body,
    adding_headers={
        'Link': f'<{ url }>; rel="first", <{ url }prev_cursor>; rel="prev"'
    },
    status=200
)

In both cases the response returned in the code has only 1 header which is {'Content-Type': 'application/json'}. Am I doing something wrong?

I stepped through the responses code with a debugger and everything seems to work fine. The Response object gets created with the right headers but when the requests module is called it doesn’t get them back.

here are my versions:

  • Python: 3.8.3
  • pytest: 5.4.3
  • requests: 2.23.0
  • responses: 0.10.14

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
XenGicommented, Jun 10, 2020

Oh man… I’m should stop drinking and coding that late. The whole thing didn’t work because I got a one off in my tests. I was looking at the wrong one the whole time. Sorry to bother you. Everything is fine.

0reactions
XenGicommented, Jun 10, 2020

I tried everything I found. I also use headers exactly like you do but they are not in the response. If you look at my example above, I’m using headers. I also tried to use the second example but with headers instead of adding_headers but it gives me the same result.

I just tried to build a smaller example and that worked:

import re
import requests
import responses


def foo():
    r = requests.get('https://example.com/foo?cursor=')
    things = []
    while 'Link' in r.headers.keys():
        things.append(r.json())
        m = re.search(r'<.+>; rel="first"(,\s+<(?P<previous>.+)>; rel="prev")?(,\s+<(?P<next>.+)>; rel="next")?', r.headers['Link'])
        if not m.group('next'):
            return things
        r = requests.get(m.group('next'))
    return things


@responses.activate
def test_foo():
    url = 'https://example.com/foo?cursor='

    responses.add(
        responses.GET,
        url=url,
        json={'foo': 1},
        headers={'Link': f'<{url}>; rel="first", <{url}next>; rel="next"'},
        status=200
    )
    responses.add(
        responses.GET,
        url=f'{url}next',
        json={'foo': 2},
        headers={'Link': f'<{url}>; rel="first", <{url}prev>; rel="prev"'},
        status=200
    )

    resp = foo()
    assert len(resp) == 2

Now I need to find out why it doesn’t in the real thing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Headers not showing in fetch response - Stack Overflow
The Header object is not empty. It is just not a regular object so it doesn't have its contents as properties on its...
Read more >
Request headers and responses - App Engine - Google Cloud
Headers removed from incoming requests; App Engine-specific headers. Request responses. Headers removed; Headers added or replaced; Response headers set in ...
Read more >
HTTP headers - MDN Web Docs - Mozilla
Response headers hold additional information about the response, like its location or about the server providing it. Representation headers ...
Read more >
How to troubleshoot the error "Failed to process response ...
The error is correct, the service did not receive any response headers or data. The key to help confirm this is to increase...
Read more >
HTTP/1.1: Status Code Definitions
The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields. 10.2.6...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found