POST Request with token

Asked

Viewed 984 times

1

  1. Start an HTTP request via POST method for the authentication URL, with the following parameters:

    • login
    • password

    sponse = http://api.dominio.com:8025/name/login? token="MY_TOKEN"

  2. The response of the request made in 1 will have as a response another URL. This URL represents the location of the instance of the system to which the user belongs, so a new request should be made for the URL you have entered.

Does anyone know how to do step two? I’m using Android Asynchronous Http Client

An example in Linux bash/shell script um exemplo em Linux bash/shell script

  • This URL there has a parameter with GET format, is that right? The token would not have to go next to the body of the post?

1 answer

5

See if it’s about that (if the token has to go inside the post):

  HttpClient httpclient = new DefaultHttpClient();
  HttpPost httppost = new HttpPost("http://api.dominio.com:8025/name/login");

  try {
     List nameValuePairs = new ArrayList();

     // Aqui setamos o token:
     nameValuePairs.add(new BasicNameValuePair("token", "ab124b3a1c2f"));
     // repita a linha de cima quantas vezes necessário,
     // com outras variaveis e parâmetros desejados.

     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     HttpResponse response = httpclient.execute(httppost);
  } catch (ClientProtocolException e) {
     // trate dos erros aqui
  } catch (IOException e) {
     // e aqui
  }

Note that I didn’t post the code using async, I put the answer only as an initial reference. Depending on the case, the async ends up disrupting the flow when it comes to interdependent requests, as you have to manage events in your code.

Edit: you may need to use it this way by putting the whole URL here:

 HttpPost httppost = new HttpPost( response );

and remove the POST token, leaving only the other necessary variables, if any:

 nameValuePairs.add(new BasicNameValuePair("variavel1", "valor1..."));
 nameValuePairs.add(new BasicNameValuePair("variavel2", "valor2..."));
  • Didn’t work, always gives error 404 not found

  • @Igorronner vc tested in a browser if the address is correct?

  • How? if it does not support GET

  • 1

    Probably won’t be a 404 if you use the wrong method, but rather another error. 404 is usually when there is no same address. And to test the address with post in the browser, just create a page with a <form action="http://api.dominio.com:8025/name/login" method="post">

  • is giving HTTP Status 401 error - Invalid token android now

  • @Is Igorronner testing 401 in your app or browser? If it’s in the app, the message is self-explanatory. If it is in the browser test, it is normal, since the token is not being passed.

  • in both tests.

  • 1

    @Igor has improved then, if 401 means the URL is correct, the request is correct, and only the token is going wrong. The question is whether the token has to go in the POST or as part of the URL, as you first exemplified. Then, it’s only the API documentation that will tell. It may be that the token needs to be in the URL, and only the rest of the variables in the POST. Details are missing on the question about this, so I’m kind of "guessing" what it might be, step by step.

  • @Bacco, thank you so much for your help! In the documentation you have the following Since I have already passed step one 1. Start an HTTP request via the POST method for the authentication URL, with the following parameters: the login the password 2. The response of the request made in 1 will have as a response another URL. This URL represents the location of the instance of the system to which the user belongs, so a new request should be made for the URL you have entered.

  • Try to edit the question by adding the necessary data in the second request, which is easier. Add what you already have ready, (minus the private data, of course), which is much easier to view and I or someone else help you. The format of this second request is very important, because the problem seems to be there.

  • @Igorronner then think just extract the token and pass correctly in the previous version, because the 401 indicates that the address was correct. But without the right API documentation, I wouldn’t know how to help.

  • But should I put the full URL with the token for the request? Did you see my last edit of the reply?

  • I did some tests and I realized that the second request is actually a get, because in the browser it returns 200 OK, but when I do this same request in the APPLICATION appears ERROR 404 Not found

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.