Error Message when connecting with API

I’m trying to connect to https://collective2.com/world/apiv3/submitSignal to submit signals for my strategy, but I get a response stating:

{
   "ok" : "0",
   "error" : {
      "title" : "Missing apikey",
      "message" : "You need to send an apikey parameter. Think you sent one? Make sure you use https. Need key?     Contact help@collective2.com"
   }
}

I’m not exactly sure how to connect to the URL and send my apikey along with my command in JSON. What I’ve been able to do so far is establish a connection and try submitting signals. However, I get this error back even before I send any requests. I’m very new to this and haven’t been able to find anything online to help with establishing a proper connection.

Here is my code for connecting:

public static void initialize() throws IOException
	{
			String httpsURL = "https://collective2.com/world/apiv3/submitSignal";
			URL myurl = new URL(httpsURL);
			con = (HttpsURLConnection)myurl.openConnection();
			con.setRequestMethod("POST");
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setRequestProperty("Content-Type","text/xml");

			output = new OutputStreamWriter(con.getOutputStream());
			input = new BufferedReader(new InputStreamReader(con.getInputStream()));
			printFromWeb(); //Where I get the error message

	}

	//Place an order
	@SuppressWarnings({ "unchecked"})
	public static void placeOrder(String action, int quantity, String symbol, String typeOfSymbol, Double LMTPrice) throws IOException
	{
		JSONObject order = new JSONObject();
		JSONObject signal = new JSONObject();
		order.put("apikey", key); //Key is initialized somewhere else
		order.put("systemid", systemID); //SystemID is initialized somewhere else
		signal.put("action", "BTO");
		signal.put("quant", quantity);
		signal.put("typeofsymbol", typeOfSymbol);
		signal.put("symbol", symbol);
		if(LMTPrice == 0)
			signal.put("market", 1);
		else
			signal.put("limit", LMTPrice);
		signal.put("duration", "GTC");
		order.put("signal", signal);

		System.out.println("Delivered order to server: "+ order.toString());
		output.write(order.toString());
		output.flush();
	}
	
	public static void printFromWeb() throws IOException
	{
		System.out.println("From Website: ");
		while(input.ready())
			System.out.println(input.readLine());
	}

I guess you should use content type “application/json”

in your command

con.setRequestProperty(“Content-Type”,“text/xml”);

Still isn’t working. Just to confirm, I tried

con.setRequestProperty("Content-Type","application/json");

I still get the same error message. I think I need to specify the apikey somewhere when I connect, but I am not sure how

This is a CURL example (broken to several lines for clarity):

curl 
-X POST 
-H "Content-Type: application/json" 
-d "{'apikey': 'YOUR_API_KEY_HERE','systemid': '11111111','signal':{'action': 'BTO','quant': 155,'symbol': 'MDLZ','typeofsymbol': 'stock','market': 1,'duration': 'DAY'}}" 
https://api.collective2.com/world/apiv3/submitSignal

It means your JSON payload must look like this:

{
  'apikey': 'YOUR_API_KEY_HERE',
  'systemid': '11111111',
  'signal':{ 'action': 'BTO',
             'quant': 155,
             'symbol': 'MDLZ',
             'typeofsymbol': 'stock',
             'market': 1,
             'duration': 'DAY'
            }
}

The best way is to start some local debugging http server, point your code to it (instead to Collective2) and see what JSON it receives.

Do I need to encapsulate the fact that I use POST and the json content type inside every message I send to the server? I think the problem may be in the fact that I don’t send any kind of message when I first connect

I do not know.

I suggest to create a JSON payload as a plain string at first (see my post) and to send it to the server without relying on serialization of the signal and order objects.

Use a single quote as a JSON data delimiter (see my post).

When you will be able to send such simple example, you will be sure that infrastructure works.
Then, you should review a result of the order.toString() command (and signal.toString()).
It must have the same structure as in my post.