2

This is my Json body :

{
    "username": {
        "country": "IN", 
        "number": "9620494812"
    }, 
    "password": "119209"
}

I'm trying to make a POST request with Okhttp as follows:

Username username = new Username("IN" , "9620494812");

JSONObject postData = new JSONObject();

try {
    postData.put("username" , username);
    postData.put("password" , "119209");

} catch (JSONException e) {
    e.printStackTrace();
}

RequestBody body = RequestBody.create(MEDIA_TYPE , postData.toString());

Request request = new Request.Builder().url("https://www.gruppie.in/api/v1/login")
        .method("POST" , body).build();

I have checked the request but i still get the error as

Response is :

{"status":401,"type":"about:blank","title":"Unauthorized"}

What am I doing wrong here?

hoefling
  • 59,418
  • 12
  • 147
  • 194
Saathwik
  • 199
  • 1
  • 2
  • 11

1 Answers1

4

As per your JSON, You have to send JSON request like this . Try this

    JSONObject postData = new JSONObject();
    JSONObject userJson=new JSONObject();
    try {
        userJson.put("country","IN");
        userJson.put("number","9620494812");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        postData.put("username" , userJson);
        postData.put("password" , "119209");

    } catch (JSONException e) {
        e.printStackTrace();
    }
Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39