Jason Vondersmith
Jason Vondersmith

Reputation: 147

Connecting to API via web vs python

I am trying to access an XML document via an API. When I attempt to connect in python I get a 403 status code. However when I paste the link into a chrome browser the data is presented as it should be. I understand that I may need some headers added to my request in Python but I am not sure how to do so.

schedule = requests.get('https://api.sportradar.us/golf-t2/schedule/pga/2015/tournaments/schedule.xml?api_key=mssbj55v2wbrbr6jcet2xcdd')
print(schedules.status_code)

I was able to get my headers in chrome but am not sure which ones I need to add to my request

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

How can I adjust my request in order to return a status of 200?

Upvotes: 2

Views: 384

Answers (1)

Eugene Lisitsky
Eugene Lisitsky

Reputation: 12845

Add all other browser headers to the request. The easiest way: open link on Chrome, open Dev Tools, Network tab, then right-click and "Copy as cURL". Paste into console and check it's enough:

$ curl 'https://api.sportradar.us/golf-t2/schedule/pga/2015/tournaments/schedule.xml?api_key=mssbj55v2wbrbr6jcet2xcdd' -H 'Accept-Encoding: gzip, deflate, sdch, br' -H 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,es;q=0.2' -H 'Upgrade-Insecure-Requests: 1' -H 'X-Compress: null' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'If-None-Match: "c97bea3f0b2917ae53554f338c416859"' -H 'Connection: keep-alive' -H 'If-Modified-Since: Wed, 07 Oct 2015 02:41:03 GMT' --compressed ;

output:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:s="http://feed.elasticstats.com/schema/golf/schedule-v1.0.xsd"
            exclude-result-prefixes="s" version="1.0">

<xsl:output method="html" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
....

Then add headers to your app with:

http://docs.python-requests.org/en/master/user/quickstart/#custom-headers

>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}

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

Upvotes: 3

Related Questions