#Login/register 308 error

54 messages · Page 1 of 1 (latest)

trail arrow
#

I have a web app + database hosted in python which I have tested and works for creating, logging in and logging out user. Now I'm trying to make the requests in my android application written in java. However, I keep getting 308 error.

Here is my RegisterActivity.java

private void register() {
        // Get headers, method and url to be used in the request
        final String user = username.getText().toString();
        final String pass = password.getText().toString();
        Map<String, String> userObject = new HashMap<>();
        userObject.put("username", user);
        userObject.put("password", pass);
        final int method = Request.Method.POST;
        final String url = "https://tddd80-app-joneri.azurewebsites.net/user";

        // Create request
        GsonRequest request = new GsonRequest(Request.Method.POST, url, userObject,
                response    -> System.out.println("Success! Response: " + response),
                error       -> System.out.println("Error with request: " + error.getMessage());

        // Add the request to the Volley request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(request);

and this is the most important part of GsonRequest which should parse JSON into string:

public class GsonRequest extends Request<String> {
    private final Map<String, String> headers;
    private final Response.Listener<String> listener;

    public GsonRequest(int method, String url, Map<String, String> headers,
                             Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.headers = headers;
        this.listener = listener;
    }

Any help is appreciated!

tall rockBOT
#

⌛ This post has been reserved for your question.

Hey @trail arrow! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

marsh wigeon
#

@trail arrow Please attach the error as well.

trail arrow
#
D/Volley: [16199] NetworkUtility.logSlowRequests: HTTP response for request=<[ ] https://tddd80-app-joneri.azurewebsites.net/user 0xce2c15ac NORMAL 1> [lifetime=5143], [size=283], [rc=308], [retryCount=1]
E/Volley: [16199] NetworkUtility.shouldRetryException: Unexpected response code 308 for https://tddd80-app-joneri.azurewebsites.net/user
marsh wigeon
trail arrow
#

dont think so

`# Create user
@app.route('/user/', methods=['POST'])
def create_user():
# Get the data from the database
data = request.get_json()
username = data['username']
password = data['password']

# Handle already existing user
user_already_exists = User.query.filter_by(username=username).first()
if user_already_exists is not None:
    return jsonify(response='Error: User already exists'),409

# Generate password hash and add it to a new user
new_user = User(username=username)
new_user.generate_password_hash(password)

# Add the new user to the database
db.session.add(new_user) 
db.session.commit()

# Return value
return jsonify(response='Successfully created a new user'), 200`
marsh wigeon
#

308 is a redirect code..

trail arrow
#

yeah, maybe i can set it to follow redirects?

marsh wigeon
#

Yes

#

Try that

trail arrow
#

how

marsh wigeon
#

How are you sending request?

#

Through which library/package?

#

HttpClient?

#

HttpRequest?

trail arrow
#

in python im using flask to send requests i think, and then im using volley to post requests, and then gson library to convert

#

but not sure, something in volley i think, sorry not 100% sure

marsh wigeon
#
@Override
public void deliverError(final VolleyError error) {
    Log.d(TAG, "deliverError");

    final int status = error.networkResponse.statusCode;
    // Handle 30x 
    if(HttpURLConnection.HTTP_MOVED_PERM == status || status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_SEE_OTHER) {
        final String location = error.networkResponse.headers.get("Location");
        Log.d(TAG, "Location: " + location);
        final GsonRequest<T> request = new GsonRequest<T>(method, location, jsonRequest, this.requestContentType, this.clazz, this.ttl, this.listener, this.errorListener);
        // Construct a request clone and change the url to redirect location.
        RequestManager.getRequestQueue().add(request);
    }
}

#

Something like this

#

Implement this in your GsonRequest..

#

Otherwise.. request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f)) Could work

#

Oh I think I know why this is happening

#

@trail arrow Change your url from https://tddd80-app-joneri.azurewebsites.net/user to https://tddd80-app-joneri.azurewebsites.net/user/

#

Or from https://tddd80-app-joneri.azurewebsites.net/user/ to https://tddd80-app-joneri.azurewebsites.net/user(PYTHON)

trail arrow
#

i dont see what changed?

marsh wigeon
#

The / in the last

trail arrow
#

E/Volley: [16230] NetworkUtility.shouldRetryException: Unexpected response code 400 for https://tddd80-app-joneri.azurewebsites.net/user/

#

now got this error

marsh wigeon
#

Did it log anything

#

Well

#

It's clear why that happened

trail arrow
#

unfortunately dont think i can do that, i think my friend is hosting the web app

#

why's that?

marsh wigeon
#
    data        = request.get_json() // Tries to load from body
    username    = data['username']
    password    = data['password']
#

You have data set at headers

#

Not body..

#

Use request.headers to access them.

trail arrow
#

ohh I see at userObject or? not seeing headers

Map<String, String> userObject = new HashMap<>();
        userObject.put("username", user);
        userObject.put("password", pass);
        final String url = "https://tddd80-app-joneri.azurewebsites.net/user/";

        // Create request
        GsonRequest request = new GsonRequest(Request.Method.POST, url, userObject,
#

because its still going into
error -> im going here
response -> not here

marsh wigeon
#
public GsonRequest(int method, String url, Map<String, String> headers,
                             Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.headers = headers;
        this.listener = listener;
    }

#

userObject is headers

trail arrow
#

yes , but userObject is a Map with <string> <string> which should take in username and password, but i dont get how i can modify so i get the "headers"

#

i have the strings
user
pass

which i want to pass as headers into my gsonrequest

// Get headers, method and url to be used in the request
        final String user = username.getText().toString();
        final String pass = password.getText().toString();
        Map<String, String> userObject = new HashMap<>();
        userObject.put("username", user);
        userObject.put("password", pass);
        final String url = "https://tddd80-app-joneri.azurewebsites.net/user/";

        // Create request
        GsonRequest request = new GsonRequest(Request.Method.POST, url, userObject,
                response    -> System.out.println("Success! Response: " + response),
                error       -> System.out.println("Error: " + error.getMessage()));

        // Add the request to the Volley request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(request);
marsh wigeon
#

or accept data form the headers instead of body in python flask

trail arrow
#

how can i post json body data?

marsh wigeon
trail arrow
#

is it possible to post the user and pass strings as parameters in my request?

#

so I should make a StringRequest instead?

#

tried what you suggested and now got 500 error,

// Get headers and url to be used in the request
        final String user = username.getText().toString();
        final String pass = password.getText().toString();
        Map<String, String> userObject = new HashMap<>();
        userObject.put("username", user);
        userObject.put("password", pass);
        final String url = "https://tddd80-app-joneri.azurewebsites.net/user/";

        StringRequest request = new StringRequest(Request.Method.POST, url,
                response -> {
                    System.out.println("some success!!!");
                },
                (Response.ErrorListener) error -> {
                    System.out.println("some error!!!");
                }) {
            @Override
            public byte[] getBody() {
                JSONObject json = new JSONObject();
                try {
                    json.put("username", user);
                    json.put("password", pass);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return json.toString().getBytes();
            }
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type","application/json");
                return headers;
            }
            @Override
            public String getBodyContentType() {
                return "application/json";
            }
        };

        // Add the request to the Volley request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(request);


E/Volley: [16370] NetworkUtility.shouldRetryException: Unexpected response code 500 for https://tddd80-app-joneri.azurewebsites.net/user/
I/System.out: some error!!!
#

oh wait i think i got it to work! thank you man 🙂

tall rockBOT