If you’re opening the url in a browser, it’ll send it as a “GET” request. You’ll need to change the http request method to “POST”, and set the url params in the request body instead of including them in the url. Checkout the call function api method in sparkjs here -
I’m not sure if that’s Java, C#, etc…, but I think you need to move the ?access_token=MYACCESSTOKEN¶ms=MY,PARAM portion into a payload that’s not a url parameter.
URL url = new URL("https://api.spark.io/v1/devices/MYCOREID/FUNCTION_NAME");
// change here
String param = "access_token=MYACCESSTOKEN¶ms=MY,PARAM";
HttpsURLConnection postCon = (HttpsURLConnection) url.openConnection();
postCon.setDoOutput(true);
postCon.setRequestMethod("POST");
postCon.setFixedLengthStreamingMode(param.getBytes().length);
postCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter sendPost = new PrintWriter(postCon.getOutputStream());
out.print(param);
out.close();
There might be other small things, like the content-type might be fine as application/json instead of a full form submit. If you want, I really recommend testing requests against requestb.in ( http://requestb.in ). They let you get a url where you can see exactly how your http request came through.
Tried it several times with the access token from the settings page (build.spark.io) and with and without /MYFUNCTION/ everytime I send the post request I receive an “400 Bad request”.
Nope can´t get it to work.
Everytime “400 - Bad request - Access Token not found”
I have reset the access token, I tried using old ones (works with javascript) but no success with posting the request through my Java File nor through DHC Chrome extension? What the …?! What exactly do I miss?
I modified my example above, function name should be a part of the url, but not part of the param string, and access_token is a param in this case and not a header, how's that?
for my understanding the old access token should result in an error. Maybe the exact same as for invalid requests?
Sorry for spamming but this is a little bit confusing.
a working example (sorry a little bit messy) but well, it works! Improvements are up to you, this is the basic Android Code for the JSON POST request. Enjoy:
class PostClient extends AsyncTask<String, Void, String> {
public String doInBackground(String... myparam) {
// Predefine variables
String myParam = new String(myparam[0]);
URL url;
try {
// Stuff variables
url = new URL("https://api.spark.io/v1/devices/MYCOREID/MYFUNCTION/");
String param = "access_token=MYACCESSTOKEN¶ms=MY,"+myParam;
Log.d(TAG, "param:" + param);
// Open a connection using HttpURLConnection
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setReadTimeout(7000);
con.setConnectTimeout(7000);
con.setDoOutput(true);
con.setDoInput(true);
con.setInstanceFollowRedirects(false);
con.setRequestMethod("POST");
con.setFixedLengthStreamingMode(param.getBytes().length);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Send
PrintWriter out = new PrintWriter(con.getOutputStream());
out.print(param);
out.close();
con.connect();
BufferedReader in = null;
if (con.getResponseCode() != 200) {
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
Log.d(TAG, "!=200: " + in);
} else {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
Log.d(TAG, "POST request send successful: " + in);
};
} catch (Exception e) {
Log.d(TAG, "Exception");
e.printStackTrace();
return null;
}
// Set null and we´e good to go
return null;
}
}
Oh btw. don´t use “application/json” as header parameter, because we send the token in the body therefore this needs to be “x-www-form-urlencoded” . You can use URLEncoder.encode() but you don´t have to. So feel free to tinker around with this!