Python API - submitSignal example

I am having a very difficult time doing a basic submitSignal using the python Requests module. Can someone please post an example. my current code is as follows, and i have tried multiple iterations of this:

def buy_stuff():

url = 'https://collective2.com/world/apiv2/submitSignal'

headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}

params = { 
	"apikey":   "XXXXXXXXX", 
	"systemid": "XXXXXXXXX", 
	"signal":{ 
	   "action": "BTO", 
	   "quant": 2, 
	   "symbol": "GOLD", 
	   "typeofsymbol": "stock", 
	   "limit": 31.05, 
	   "duration": "GTC", 
	   "signalid": 1011 
	} 
}

r = requests.post(url, params=params, headers=headers)

Any help is appreciated!

What is the response you get when you try this?

I get an error that says something along the lines of “bad JSON request, dangling commas, etc.”…

Well, I don’t know Python, but I do know Google, and this StackOverflow post seems to indicate you need to do something like this:

data = { 
	"apikey":   "XXXXXXXXX", 
	"systemid": "XXXXXXXXX", 
	"signal":{ 
	   "action": "BTO", 
	   "quant": 2, 
	   "symbol": "GOLD", 
	   "typeofsymbol": "stock", 
	   "limit": 31.05, 
	   "duration": "GTC", 
	   "signalid": 1011 
	} 
}

params={}

requests.post(url, params=params, json=data);

(If you are using a version of requests that is less than 2.4.2, you need to manually do the encoding:

requests.post(url, params=params, data=json.dumps(data), headers);

But - assuming you are using the latest requests version - I think the main issue is probably that you need to use the json keyword to tell requests to encode for you.

I apologize in advance that I am a Python novice and everything I wrote in this post may be incorrect…

Matthew

This worked perfectly! I had two issues: my requests version was an old one, and I was not using the ‘data’/ ‘json’ inputs.

Thanks!
Evan