Search code examples
python-3.xpython-requestsurllib

MissingSchema - Invalid URL Error with Requests module


Trying to get data from the eBay API using GetItem. But requests isn't reading or getting the URL properly, any ideas? Getting this error:

requests.exceptions.MissingSchema: Invalid URL '<urllib.request.Request object at 0x00E86210>': No schema supplied. Perhaps you meant http://<urllib.request.Request object at 0x00E86210>?

I swear I had this code working before but now it's not, so I'm not sure why.

My code:

from urllib import request as urllib
import requests

url = 'https://api.ebay.com/ws/api.dll'

data = '''<?xml version="1.0" encoding="utf-8"?>
<GetItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>my-auth-token</eBayAuthToken>
</RequesterCredentials>
<ItemID>any-item-id</ItemID>
</GetItemRequest>'''

headers = {
    'Content-Type' : 'text/xml',
    'X-EBAY-API-COMPATIBILITY-LEVEL' : 719,
    'X-EBAY-API-DEV-NAME' : 'dev-name',
    'X-EBAY-API-APP-NAME' : 'app-name',
    'X-EBAY-API-CERT-NAME' : 'cert-name',
    'X-EBAY-API-SITEID' : 3,
    'X-EBAY-API-CALL-NAME' : 'GetItem',
}

req = urllib.Request(url, data, headers)
resp = requests.get(req)
content = resp.read()

print(content)

Thank you in advance. Any good reading material for urllib would be great, too.


Solution

  • You are mixing the urllib and the requests library. They are different libraries that can both do HTTP-requests in Python. I'd suggest you use only the requests library.

    Remove the line req = urllib.Request(url, data, headers) and replace the resp = ... line with:

    r = requests.post(url, data=data, headers=headers)
    

    Print the response body like this:

    print(r.text)
    

    Check out the Requests Quickstart here for more examples: https://2.python-requests.org//en/master/user/quickstart/