OkHTTP Post Error 405 - android

I'm trying to send data from an Android device to a web server using Flask.
I'm just testing by returning some strings.
If I do a post request on POSTMAN to IP_Address/playlists with {"spotify_token":"test token"} I'll get the proper response. {"result":"received token"}
But if I try to send a similar HTTP Post request on Android with OkHTTP I get error 405. 192.168.0.105 - - [19/Jul/2018 17:23:37] "POST //playlists HTTP/1.1" 405 -
Response from server:
Allow: HEAD, GET, OPTIONS
Content-Length: 178
Server: Werkzeug/0.14.1 Python/3.7.0
Date: Fri, 20 Jul 2018 01:57:56 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
I checked my code with other answers and as far as I can tell it looks correct.. Also, I'm able to do a GET request in my home page fine.
Android Code
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public void onClick(View view){
client = new OkHttpClient();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("UserId", "test token");
} catch (JSONException e) {
e.printStackTrace();
}
RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString());
Request request = new Request.Builder()
.url(IP_ADDRESS + "/playlists")
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
Log.e("ExportButton", e.toString());
}
#Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("ExportButton", response.body().string());
if(response.isSuccessful()){
final String myResponse = response.body().string();
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
messageWindow.setText(myResponse);
}
});
}
}
});
Flask code
#app.route('/playlists', methods =["POST"])
def get_playlists():
data = request.get_json()
spotify_token = data['spotify_token']
print(spotify_token)
return jsonify({'result': 'received token'})

I printed out the response and found the full URL it was going to was "http://IP_Address//playlists" (there were two "/"s which is why it was getting the error.
Thanks Mushtu.

Related

Making GET Request after a POST Request in the same Session with OkHttp in Android Studio

I am trying to retrieve some JSON data using OkHttp in Android Studio from the URL: www.duolingo.com/vocabulary/overview
Before I can get the data using this URL, it requires me to Login into the Duolingo server first (which makes sense since I want it to return data from my profile) so I make a POST request with my credentials using OkHttp. This is my code to achieve that:
OkHttpClient client = new OkHttpClient();
String postUrl = "https://www.duolingo.com/2017-06-30/login?fields=";
String getUrl = "https://www.duolingo.com/vocabulary/overview";
String credentials = "{\"identifier\": \"something#email.com\", \"password\": \"Password\"}";
RequestBody body = RequestBody.create(credentials, MediaType.parse("application/json; charset=utf-8"));
Request request = new Request.Builder()
.url(postUrl)
.post(body)
.build();
client.newCall(request).enqueue(new Callback()
{
#Override
public void onFailure(#NotNull Call call, #NotNull IOException e)
{
Log.i("TAG", "ERROR - " + e.getMessage());
}
#Override
public void onResponse(#NotNull Call call, #NotNull Response response) throws IOException
{
if (response.isSuccessful())
{
Log.i("LOG", "LOGGED IN");
}
else
{
Log.i("LOG", "FAILED - " + response.toString());
}
}
});
The Response is successful and I get a 200 Response Code.
Now since I have Logged In, I want to make a GET Request to the URL mentioned above to get the JSON Data. Problem is I do not know how to make a GET Request in succession to the POST Request I just made. OkHttp treats the 2 consecutive requests as separate while I want them to be treated as the same session.
Someone told me Cookies can help but I am totally oblivious to that and how they work. All help is appreciated!

retrofit , okhttp3 add header

I need to get the XML file from the site. I'm learning to use Retrofit.
I need to make a request and attach my API key via the "X-AppId" header. It should look like this:
X-AppId: my key.
If I do this from the browser, I get the answer.
Through the retrofit I get the access
error 403 Forbidden code = 403, message = Forbidden, url = https: //
Tell me how it is implemented properly to receive an answer from the server code = 200
Here is my implementation:
public interface myAPIinterface {
#GET("/api/ru/index/route/?from=Minsk&to=Warsaw")
Call<Routes> getProducts();
}
This is the activity where I output to the log:
private void getProducts(){
final ProgressDialog loading = ProgressDialog.show(this,"Fetching Data","Please wait...",false,false);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Log.d(TAG, "getProducts");
httpClient.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader("X-AppId:", "97377f7b702d7198e47a2bf12eec74")
.build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://rasp.rw.by")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
myAPIinterface api = retrofit.create(myAPIinterface.class);
Call<Routes> call = api.getProducts();
call.enqueue(new Callback<Routes>() {
#Override
public void onResponse(#NonNull Call<Routes> call, #NonNull Response<Routes> response) {
Log.d(TAG, "onResponse");
Log.d(TAG, String.valueOf(kk));
Log.d(TAG, String.valueOf(response));
loading.dismiss();}
#Override
public void onFailure(Call<Routes> call, Throwable throwable) {
loading.dismiss();
Log.d(TAG, "onFailure" + throwable);
}
});
this is a log:
Response{protocol=http/1.1, code=403, message=Forbidden,
url=https://rasp.rw.by/api/ru/index/route/?from=Minsk&to=Warsaw}
if I take third-party sites where there are no headers, I get a response of 200 without problems. What am I doing wrong in this case? Thank you.
Oh, man, what are you doing. You can use annotations like #Query, #Header, etc.
public interface myAPIinterface {
#GET("/api/ru/index/route")
Call<Routes> getProducts(#Header("X-AppId:") String YOUR_APP_ID,
#Query("from") String from,
#Query("to") String to)
}
Then you can create request like this:
Retrofit retrofit = new Retrofit.Builder().
.baseUrl("https://rasp.rw.by")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
retrofit.create(myAPIinterface.class).getProducts(myId, "Minsk", "Warsaw").enqueue ...
How It can help? You forgot to add header at second retrofit and then you have 403 error. So, You must add annotations, and this will be the last mistake when you forgot to put value to header/query/etc.

Sending String to server using OkHttpClient POST Method in Service Android

I am sending Json String to my webapi php file from Android Using OkHttpClient from background Service.
The Code of Web Service Is
if (isset($_POST)) {
$data = json_decode(file_get_contents('php://input'), true);
}
The Android Code is :
String requestString = jsonObject.toString();
URL url = new URL("http://cbs.octa.in/webapis/InsertData.php");
OkHttpClient client = new OkHttpClient();
MediaType mdt = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, requestString);
Request request = new Request.Builder()
.url(url)
.header("Content-Type","application/json")
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
Log.v("BGAPI", "RESPONSE ERROR " + e.getMessage());
}
#Override
public void onResponse(Call call, Response response) throws IOException {
Log.v("BGAPI", "RESPONSE " + response);
}
});
The output for this call i am getting is :
RESPONSE ERROR Unable to resolve host "cbs.octa.in": No address associated with hostname
I tried this call from postman and its working , even i confirmed by entering the same url in broweser.
My question is to send data to server in that web api. Please guide me.
Note : For Privacy i have not given the actual domain name.

Moodle Login with an external Android App (Okhttp)

Thanks for your time. Im programming an Android App for School and need a Authentification Form for the Moodle Login (Server ends with /index.php).
My Goal: Get the "Set-Cookie" Header in the Response with the active Cookie in it. This is given, if the Server Status returnes "303(See Other)".
My Question: What should I post to the Login Server, to get the Status "303" (and therefore also the right Cookie) ?
I dont know, if the ".add" Method is the right or wrong or if I should send more or less to the Server.
class MoodleLogin{
public static void FirstRequest(String url) throws Exception {
final OkHttpClient client = new OkHttpClient();
//FORM BODY
RequestBody formBody = new FormBody.Builder()
.add("username", USERNAME) // Username
.addEncoded("password", PASSWORT) //Passwort
.add("token", "6f65e84f626ec97d9eeee7ec45c88303") //Security Key for Moodle mobile web service (I dont know if I need that)
.build();
// A normal Request
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
// Asynchronous Call via Callback with onFailure and onResponse
client.newCall(request).enqueue(new okhttp3.Callback() {
#Override
public void onFailure(Call call, IOException e) {
Log.i("Internet", "request failed: " + e.toString());
}
#Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) { // Server Status is not 200 - THAT IS WHAT I WANT
Log.d("Server Status", response.message().toString());
throw new IOException("Unexpected code ");
} else { // Server Status is 200(OK)
Log.d("Server Status", response.message().toString());
}
response.body().close();
}
}); // End of "onResponse"
}
This peace of Code only returns the Server Status "200" (what is wrong in my case).
Do anyone know, what I must change to get the Status "303" ? I tested it with hurl.it (A Website for HTTP Requests) and it works only if I post the "username" and "password" like normal Parameters.
Im grateful for every answer and tip !
I answered it myself. So here is the right code:
Connection.Response res = Jsoup
.connect("your Moodle-Login Page")
.data("username", username, "password", password)
.method(Connection.Method.POST)
.execute();
String Login_cookies = res.cookies().toString();
String cookie = Login_cookies.substring(1, 50);
System.out.println("COOKIES: "+cookie);

Sending login params to Rails server using Volley

Link to Rails server. https://afternoon-sea-5654.herokuapp.com.
I want to send a simple JSON POST to attempt to login. I get the following volley errors
Error
04-09 14:03:56.156 3002-3031/com.digitalnatives.volleytest E/Volley﹕ [244] BasicNetwork.performRequest: Unexpected response code 500 for https://afternoon-sea-5654.herokuapp.com/sessions/create
04-09 14:03:56.160 3002-3002/com.digitalnatives.volleytest E/Volley﹕ [1] 4.onErrorResponse: Error:
I can't tell if it's down to the way I am formatting the request. Here is an example login request using curl manually.
Login -
curl -X POST -d "user[email]=ywaghmare5203#gmail.com&user[password]=12345678&" https://afternoon-sea-5654.herokuapp.com/sessions/create.json
Request perameter: email, password,
Response Perameter:
{"id":10,"username":"yogeshwaghmare1","email":"ywaghmare5203#gmail.com","password_hash":"$2a$10$pvLhzJlVz8Hl86O7N/ekiO2wrwNxbfTZlYPtccY4f7vXYNFs1vq6a","password_salt":"$2a$10$pvLhzJlVz8Hl86O7N/ekiO","last_login_time":null,"is_active":null,"contact_number":"123456","created_at":"2015-04-01T19:20:37.552Z","updated_at":"2015-04-01T19:20:37.552Z"}
JSONObjectRequest code
public void loginTest() {
private final String LOGIN = "https://afternoon-sea-5654.herokuapp.com/sessions/create";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
// old test code : params.put("user[email]=test1#gmail.com&", "user[password]=12345678&");
params.put("user[email]", "test1#gmail.com");
params.put("user[password]", "12345678");
JsonObjectRequest req = new JsonObjectRequest(LOGIN, new JSONObject(params),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
responseText.setText("Worked!");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
responseText.setText("Nope");
}
});
// add the request object to the queue to be executed
AppController.getInstance().addToRequestQueue(req);
}
Actually in response you are not Getting the JSON data. It is returning a HTML message regarding redirection. Your response is 302
Response code 500 means its a server sided syntax error. You can test your api through online API testing platform like Runscope. It really save our time and confusion when we collaborate with web team and android team.
Runscope Link

Categories

Resources