J.Doe
J.Doe

Reputation: 107

Solution for the Invalid URL non-numeric port error?

I am trying to use a cryptcurrency API to get some information from a remote server in python. The example from the API how to do it is here: https://developers.cryptoapis.io/technical-documentation/blockchain-data/unified-endpoints/get-transaction-details-by-transaction-id

But when I try to run it I get an exception

Exception has occurred: InvalidURL nonnumeric port: '//rest.cryptoapis.io/v2'

I am not sure what is wrong here (new to Python). Can someone please point out? I thought at least the formal example from the API provider must work?

My code is:

import http.client
conn = http.client.HTTPConnection("https://rest.cryptoapis.io/v2")
headers = {
  'Content-Type': "application/json",
  'X-API-Key': "API key provided by the software provider"
}
conn.request("GET", "blockchain-data,bitcoin,testnet,transactions,4b66461bf88b61e1e4326356534c135129defb504c7acb2fd6c92697d79eb250", headers=headers )

res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Upvotes: 0

Views: 409

Answers (1)

user2668284
user2668284

Reputation:

Looks like you misunderstood the documentation. You may find this helpful:-

import requests
import json

APIKEY = 'YourAPIKeyGoesHere' # <-----
BASE = 'https://rest.cryptoapis.io/v2'
BLOCKCHAIN = 'bitcoin'
NETWORK = 'testnet'
TID = '4b66461bf88b61e1e4326356534c135129defb504c7acb2fd6c92697d79eb250'
with requests.Session() as session:
    h = {'Content-Type': 'application/json',
         'X-API-KEY': APIKEY}
    r = session.get(
        f'{BASE}/blockchain-data/{BLOCKCHAIN}/{NETWORK}/transactions/{TID}', headers=h)
    r.raise_for_status()
    print(json.dumps(r.json(), indent=4, sort_keys=True))

Upvotes: 1

Related Questions