Sending HTTP requests

Sending HTTP requests

The Allele Registry API is based on HTTP requests. There are three types of HTTP requests used by this API: GET, POST and PUT. All HTTP GET requests can be send with the use of any Internet browser (just copy the URL to the address bar), but for sending POST and PUT requests some more advanced tool is needed. Moreover, all PUT requests requires authentication, what is described in the next section. Ready scripts for sending POST and PUT requests with payload loaded from given file can be found at https://reg.genome.network/doc/scripts.

Below you can find examples how to send GET, POST and PUT requests from bash console and from chosen programming languages. Variables “login” and “password” must be set in advance.

Bash

These sections contain sequence of commands which may be run from bash console (by copy & paste).They require some additional tools like curl or sha1sum and depend on standard tools like echo, cut, etc… Different behavior of these dependencies may perturb some examples. However provided code snippets should work on the majority of modern Linux distributions.

# ====== send a GET request with parameter
URL="http://reg.test.genome.network/allele?hgvs=NC_000010.11:g.87894077C>T"
# convert symbol > to special code %3E
URL=${URL//>/%3E}
curl -X GET "${URL}"

# ====== send a POST requests with parameter and payload taken from the file alleles.txt
URL="http://reg.test.genome.network/alleles?file=hgvs"
curl -X POST "${URL}" --data-binary @alleles.txt

# ====== calculate authentication parameters and send a PUT request
# you have to set LOGIN and PASSWORD here
URL="http://reg.test.genome.network/allele?hgvs=NC_000010.11:g.87894077del"
IDENTITY=`echo -n "${LOGIN}${PASSWORD}" | sha1sum | cut -d \ -f 1`
TIME=`date +%s | tr -d "\n"`
TOKEN=`echo -n "${URL}${IDENTITY}${TIME}" | sha1sum | cut -d \ -f 1`
REQUEST="${URL}&gbLogin=${LOGIN}&gbTime=${TIME}&gbToken=${TOKEN}"
curl -X PUT "${REQUEST}"

Ruby

All included ruby code snippets should work with ruby version >= 1.8.7.

require 'net/http'
require 'digest/sha1'

# ====== send a GET request with parameter
url = 'http://reg.test.genome.network/allele?hgvs=NC_000010.11:g.87894077C>T'
url = URI.escape(url) # convert symbol > to special code %3E
http = Net::HTTP.new(URI(url).host)
req = Net::HTTP::Get.new(url)
res = http.request(req)
print res.body

# ====== send a POST requests with parameter and payload taken from the file alleles.txt
url = 'http://reg.test.genome.network/alleles?file=hgvs'
http = Net::HTTP.new(URI(url).host)
req = Net::HTTP::Post.new(url)
req.body = File.open('alleles.txt').read
res = http.request(req)
print res.body

# ====== calculate authentication parameters and send a PUT request
# you have to set login and password here
url = 'http://reg.test.genome.network/allele?hgvs=NC_000010.11:g.87894077del'
identity = Digest::SHA1.hexdigest("#{login}#{password}")
gbTime = Time.now.to_i.to_s
token = Digest::SHA1.hexdigest("#{url}#{identity}#{gbTime}")
request = "#{url}&gbLogin=#{login}&gbTime=#{gbTime}&gbToken=#{token}"
http = Net::HTTP.new(URI(url).host)
req = Net::HTTP::Put.new(request)
res = http.request(req)
print res.body

Python

Python code snippets should work with python version >= 2.7. The library “request” is not a part of the Python Standard Library and probably must be installed separately (in Linux it should be available through default package manager).

import requests
import hashlib
import time

# ====== send a GET request with parameter
url = 'http://reg.test.genome.network/allele?hgvs='
# convert symbol > to special code %3E
url += requests.utils.quote("NC_000010.11:g.87894077C>T")
res = requests.get(url)
print(res.text)

# ====== send a POST requests with parameter and payload taken from the file alleles.txt
url = 'http://reg.test.genome.network/alleles?file=hgvs'
res = requests.post(url, data=open('alleles.txt').read())
print(res.text)

# ====== calculate authentication parameters and send a PUT request
# you have to set login and password here
url = 'http://reg.test.genome.network/allele?hgvs=NC_000010.11:g.87894077del'
identity = hashlib.sha1((login + password).encode('utf-8')).hexdigest()
gbTime = str(int(time.time()))
token = hashlib.sha1((url + identity + gbTime).encode('utf-8')).hexdigest()
request = url + '&gbLogin=' + login + '&gbTime=' + gbTime + '&gbToken=' + token
res = requests.put(request)
print(res.text)