Python Forum

Full Version: Adding Parameters to an API Request
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Could someone kindly advise on how to add a parameter to an API request, this maybe using curl, but I am unsure of how to do this.

I am using Windows 10 and Python 3.6.

The first question is, how to add an option provided by the website to request the time to be in Unix format (i.e. 1495973084.8393297) rather than the current format of (2016-12-31T22:00:00.000000Z) as I need the format to be consistant,

The website provides this parameter X-Accept-Datetime-Format: UNIX but I am unsure how to include it in the url string successfully.

The second question is, if the solution involves converting the time format rather than requesting the unix format, then I would still like to understand how it can be done via an API as I have other requirements to add parameters to the API.

Thank you in advance. A copy of the code is shown below.

Bass

######################
# Libraries, Passed Variables etc.  #
######################
import requests as requests
import json as json

url_pair = "GBP_JPY"

#################
# Initilise url for API Call #
#################

# Standard API Set up
url_granularity = ""
url = ""
url_1 = 'https://api-fxtrade.oanda.com/v1/candles?instrument='
url_2 = '&granularity='
url_3 = '&count='
url_4 = '&candleFormat=bidask'

# THIS IS what I have tried
# url_4 = url_4 + '? X-Accept-Datetime-Format: UNIX'

# Customizing Options for API
url_granularity = "M"
url_count = "5"
url = url_1 + url_pair + url_2 + url_granularity + url_3 + url_count + url_4
url_response = requests.get(url)
M_pdata = json.loads(url_response.text)
print("url = ", url)
print("M_pdata = ", M_pdata)
before dive into this, is there any specific reason not to use the official python wrapper around the api?
Hi buran,

Thank you for your reply.

The main broker I use is IG and once I have worked out how to add parameters, I will be switching my API to use IG as my primary API and using the Oanda as a secondary one (The Oanda one does not need authorization, which is why I have started with this one) - As you may kknow, the API also allows you to carry out trades and other account specific tasks, hence the need to use specific API's.

I have looked at various wrapper but am having difficulty understanding them in detail, so have opted to start off with a basic version and add to it as and when it makes sense to my understanding.

Thank you for your feedback.

Bass
(May-28-2017, 04:25 PM)Bass Wrote: [ -> ]The Oanda one does not need authorization, which is why I have started with this one
based on the docs, authentication is required to access live accounts http://developer.oanda.com/rest-live/authentication/

as to the params:
http://docs.python-requests.org/en/maste...rs-in-urls
http://docs.python-requests.org/en/maste...om-headers

That's not a url parameter, it's an http header.
headers = {"X-Accept-Datetime-Format": "UNIX"}
url_response = requests.get(url, headers=headers)