3

I'm relatively new to Python so would like some help, I've created a script which simply use the request library and basic auth to connect to an API and returns the xml or Json result.

# Imports
import requests
from requests.auth import HTTPBasicAuth

# Set variables 
url = "api"
apiuser = 'test'
apipass = 'testpass'

# CALL API
r = requests.get(url, auth=HTTPBasicAuth(apiuser, apipass))

# Print Statuscode
print(r.status_code)

# Print XML
xmlString = str(r.text)
print(xmlString)

if but it returns a blank string.

If I was to use a browser to call the api and enter the cretentials I get the following response.

<Response>
<status>SUCCESS</status>
<callId>99999903219032190321</callId>
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Dummy">
<authorFullName>jack jones</authorFullName>
<authorOrderNumber>1</authorOrderNumber>
</result>
</Response>

Can anyone tell me where I'm going wrong.

chinskiy
  • 2,557
  • 4
  • 21
  • 40
MrG
  • 55
  • 1
  • 1
  • 8
  • Are you sure you need `HTTPBasicAuth`? – SuperStew Jan 31 '18 at 15:00
  • What does `print(r.status_code)` outputs ? Have you tried `print r.content` ? – IMCoins Jan 31 '18 at 15:06
  • the status code is 200 which is what I'm finding confusing and if I print the r.content it returns b''.....its version 3 so print r.content doesn't work so I use print(r.content) if thats correct – MrG Jan 31 '18 at 15:13

5 Answers5

1

What API are you connecting to?

Try adding a user-agent to the header:

r = requests.get(url, auth=HTTPBasicAuth(apiuser, apipass), headers={'User-Agent':'test'})
Andrew Seaman
  • 290
  • 3
  • 8
1

Although this is not an exact answer for the OP, it may solve the issue for someone having a blank response from python-requests.

I was getting a blank response because of the wrong content type. I was expecting an HTML rather than a JSON or a login success. The correct content-type for me was application/x-www-form-urlencoded.

Essentially I had to do the following to make my script work.

data = 'arcDate=2021/01/05'
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
}

r = requests.post('https://www.deccanherald.com/getarchive', data=data, headers=headers)
print(r.status_code)
print(r.text)

Learn more about this in application/x-www-form-urlencoded or multipart/form-data?

lu5er
  • 3,229
  • 2
  • 29
  • 50
0

Run this and see what responses you get.

import requests

url = "https://google.com"

r = requests.get(url)

print(r.status_code)
print(r.json)
print(r.text)

When you start having to pass things in your GET, PUT, DELETE, OR POST requests, you will add it in the request.

url = "https://google.com"
headers = {'api key': 'blah92382377432432')

r = requests.get(url, headers=headers)

Then you should see the same type of responses. Long story short, Print(r.text) to see the response, then you once you see the format of the response you get, you can move it around however you want.

Tyler C.
  • 141
  • 1
  • 4
  • Thanks guys, I've sorted the issue not, it was down to the auth type, it required digest auth after all # CALL API r = requests.get(url, auth=HTTPDigestAuth(apiuser, apipass)) – MrG Feb 13 '18 at 11:05
0

I have an empty response only when the authentication failed or is denied. The HTTP status is still ≤ 400. However, in the header you can find :

'X-Seraph-LoginReason': 'AUTHENTICATED_FAILED'

or

'X-Seraph-LoginReason': 'AUTHENTICATED_DENIED'
Sheeran D
  • 61
  • 1
  • 6
-1

If the request is empty, not even a status code I could suggest waiting some time between printing. Maybe the server is taking time to return the response to you.

import time time.sleep(5)

Not the nicest thing, but it's worth trying

How can I make a time delay in Python?

I guess there are no errors during execution

EDIT: nvm, you mentioned that you got a status code, I thought you were literally geting nothing.

On the side, if you are using python3 you have to use Print(), it replaced Print

ipop
  • 34
  • 4
  • Sorry i'd printed the r.status_code to see what was being returned to see if there was anything else which was causing the error. I've tried the time.sleep option and it not worked sadly, I'm confused as it returned data to a web browser quite quickly 1 second max. but the calling the data via python results in nothing . – MrG Jan 31 '18 at 15:46
  • print(r.content) also returns blank? could you provide the API you are using if it's a public one? – ipop Feb 01 '18 at 15:52