0

In my view, I want to redirect to a URL (which points to a hosted image) but also add the User-Agent header to that GET request (to avoid 403 errors). I've explored two options:

  1. The redirect(url) Django function. Is there a way to somehow add on headers?
  2. Using the requests library:

    r = requests.get(picture.url, headers={'User-Agent': user_agent,})
    

    But then what should I return from my view? return r, return r.content or return json() didn't work for me.

== EDIT AFTER DUPLICATE QUESTION SOLUTION ==

As suggested, I tried the solution as shown here:

def my_view(request):
    response = redirect("www.somewebsite.com/image.png")
    response['User-Agent'] = "Mozilla..."
    return response

But that didn't help with the 403 error when fetching image.png. I want to make sure that headers are added to the GET request fetching the image, not to the response returned by the view.

Thanks!

tsotsi
  • 683
  • 2
  • 8
  • 20
  • Does this answer your question? [Add request header before redirection](https://stackoverflow.com/questions/31785096/add-request-header-before-redirection) – Linh Nguyen Nov 07 '19 at 06:54
  • Thanks, I tried it before but it doesn't seem to work in my scenario. Please see my edit above. – tsotsi Nov 07 '19 at 17:22

0 Answers0