Python Forum
Adding Parameters to an API Request
Thread Rating:
  • 5 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding Parameters to an API Request
#1
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)

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#2
before dive into this, is there any specific reason not to use the official python wrapper around the api?
Reply
#3
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

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#4
(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
Reply
#5
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how can I correct the Bad Request error on my curl request tomtom 8 5,058 Oct-03-2021, 06:32 AM
Last Post: tomtom
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 3,922 Jun-18-2020, 08:07 AM
Last Post: buran
  Adding markers to Folium map only adding last element. tantony 0 2,120 Oct-16-2019, 03:28 PM
Last Post: tantony

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020