0

I am using requests and I'm trying to get the response (as seen on Network tab on chrome)

url = r"https://wwww.mylocal.com/345523"
authKey = base64.b64encode("testu:pwd$u")
headers = {"Content-Type":"application/json", "Authorization":"Basic " + "YWRtaW46OWJKTmp5WUxKc1A0dlJ1dTd2Mk0"}
data = { "id":444}
r = requests.get(url, headers=headers, params = data)
print r.content

But I keep getting the HTML of the page and I want to get actual response. When using r.content I get a message that object cannot be decoded, which makes sense since r.content retrieves the source code instead of actual response. Is there a way to get that? The response I see on network tab is similar to:

{"search_results": {"@xmlns": "http://mylocal.com/345523", "ColorsInfo": {"bgColor": "552448"}}

And what I want to retrieve is the value of bgcolor but on r.content I get the Html content of the page.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
...and so on

2 Answers 2

1

solved it! Parameter has to be a string so correct code:

url = r"https://wwww.mylocal.com/345523"
authKey = base64.b64encode("testu:pwd$u")
headers = {"Content-Type":"application/json", "Authorization":"Basic " + "YWRtaW46OWJKTmp5WUxKc1A0dlJ1dTd2Mk0"}
data = { "id":"444"}
r = requests.get(url, headers=headers, params = data)
print r.content
Sign up to request clarification or add additional context in comments.

Comments

0

My first guess is that the think you are getting is a login form because you have not properly logged into the site.

Can you verify this is the case by reading through the html or writing the html into a file and opening it up in the browser.

If my suspicions are correct look at requests session object (http://docs.python-requests.org/en/latest/user/advanced/), post the username and password via form as you would in the browser and then request the data above.

3 Comments

Thanks for the answer, but credentials are ok, the html on response is the content of the homepage so no login form
In Chrome's Network tab can you take a screenshot of the Request Headers section in the Headers tab of the request you're making from Python?
Can you update your code to use requests' auth=(username, password) kwarg rather than placing it in the header yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.