Skip to content

Scripting: Decode Responses

ens-bwalts edited this page Feb 5, 2020 · 1 revision

Overview

Content-type and Accept

HTTP allows the serving of different representations of a resource based on client preferences.

Content-type and Accept headers are how servers and clients negotiate what format they will communicate with. Examples are text/html, text/plain, application/json, image/png, etc.

The returned content-types can be specified in the header as accept (you will need to use content-type in URLs).

The endpoint documentation pages list allowed content-types.

This page lists how you specify output formats:

https://github.com/Ensembl/ensembl-rest/wiki/Output-formats

Decoding Examples

Here are some decoding examples in Python, Perl and R.

In most cases you will be using JSON formatted responses.

Most languages have JSON parsers that return the data as a structure.

Decode the Response – Python

In Python, pretty print (pprint) will give you a more human readable format.


decoded = r.json()
pprint (decoded)

Decode the Response – R

In R prettify will give you a more human readable format.


decoded = content(r, "text")
prettify (decoded)

Decode the Response – Perl

In Perl Data::dumper (dumper) will give you a more human readable format.


my $decoded = Dumper decode_json($r->{content});
print $decoded;