could'nt Get Session data through api request

Srujan Kumar 8 years ago

i am using api/session to create a new session.
but i coudnt get the session data again when i use api/session GET request.
getting below error
java.io.FileNotFoundException: http://demo.traccar.org/api/session

Anton Tananaev 8 years ago

It means that you are not using session cookie.

Srujan Kumar 8 years ago

Hi Anton,
this time iam getting 406 status code on post session when using requestProperty "application/x-www-form-urlencoded".
what else iam missing

Anton Tananaev 8 years ago

I would recommend to compare your requests with what official web app sends.

Srujan Kumar 8 years ago

please check my request parameters
I am getting 400

String emailid = email.getText().toString().trim();
String pwd = password.getText().toString().trim();
URL url = new URL(http://demo.traccar.org/api/session);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.addRequestProperty( "Content-type", "application/x-www-form-urlencoded; charset=UTF-8");    
connection.addRequestProperty( "Access-Control-Allow-Origin", "*");
connection.addRequestProperty("X-Requested-With", "XMLHttpRequest");
connection.addRequestProperty("Connection", "keep-alive");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
JSONObject jsonParam = new JSONObject();
jsonParam.put("email", emailid);
jsonParam.put("password", pwd);
DataOutputStream os = new DataOutputStream(connection.getOutputStream());
os.writeBytes(jsonParam.toString());
os.flush();
final int httpResponseCode = connection.getResponseCode();
if(httpResponseCode==200) {

    startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
else
{
    runOnUiThread(new Runnable()
    {
        public void run()
        {
            Toast.makeText(getApplicationContext(),httpResponseCode+"",Toast.LENGTH_SHORT).show();

        }
    });
}
Anton Tananaev 8 years ago

Why are you sending JSON? I would recommend you to carefully read API documentation. Also, if it doesn't work, compare your request with official web app.

Srujan Kumar 8 years ago

yes now it is working with form data.
thanks.

Srujan Kumar 8 years ago

hi anton,

can i reset a specific users password using PUT /users/{id}
do i need admin access for that too?
and do i need to send the whole user model to update any single value?

Srujan Kumar 8 years ago

one more thing i am getting the user data using session Api

Anton Tananaev 8 years ago

User can update its own password. Admin can update any user password. You need to send full model.

Srujan Kumar 8 years ago

I am getting 400.
I tried to compare my request with offical web app but i coudn't find them in web app.
When trying to reset password no request is generated in official web app. Where can i see them.
below is what iam sending but getting 400 error. Am i doing anything wrong or missing any parameters.

URL url = new URL(http://demo.traccar.org/api/users/"LoginActivity.userId");

final String basicAuth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                HttpURLConnection connection
                        = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("PUT");
                connection.setRequestProperty("Authorization", basicAuth);
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setDoOutput(true);
                connection.connect();
                JSONObject jsonParam = new JSONObject();
                jsonParam.put("id", LoginActivity.userId);
                jsonParam.put("attributes", LoginActivity.uattributes);
                jsonParam.put("name", LoginActivity.uname);
                jsonParam.put("login", LoginActivity.ulogin);
                jsonParam.put("email", LoginActivity.uemail);
                jsonParam.put("phone", LoginActivity.uphone);
                jsonParam.put("readonly", LoginActivity.ureadonly);
                jsonParam.put("admin", LoginActivity.uadmin);
                jsonParam.put("map", LoginActivity.umap);
                jsonParam.put("latitude", LoginActivity.ulatitude);
                jsonParam.put("longitude", LoginActivity.ulongitude);
                jsonParam.put("zoom", LoginActivity.uzoom);
                jsonParam.put("twelveHourFormat", LoginActivity.utwelveHourFormat);
                jsonParam.put("coordinateFormat", LoginActivity.ucoordinateFormat);
                jsonParam.put("disabled", LoginActivity.udisabled);
                jsonParam.put("expirationTime", LoginActivity.uexpirationTime);
                jsonParam.put("deviceLimit", LoginActivity.udeviceLimit);
                jsonParam.put("userLimit", LoginActivity.uuserLimit);
                jsonParam.put("deviceReadonly", LoginActivity.udeviceReadonly);
                jsonParam.put("token", LoginActivity.utoken);
                jsonParam.put("limitCommands", LoginActivity.ulimitCommands);
                jsonParam.put("password", newapwd);


                OutputStreamWriter out = new OutputStreamWriter(
                        connection.getOutputStream());
                out.write(jsonParam.toString());
                out.flush();
                out.close();
                connection.getInputStream();

                int code = connection.getResponseCode();
                Log.d("tesing", "I." + code);
                if (code == 200) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(getApplicationContext(), "password changed", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
Anton Tananaev 8 years ago

You can check requests in the browser developer console.