Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass string in 'Body' Parameter of Retrofit 2 in android

@POST("api/login")
Call<ApiResponse> loginUser(@Body String user);

Here the string is actually a JSONstring i.e.

{"email":"[email protected]","password":"test"}

Couldnt figure out what is wrong in this. Either the string it again converted to json. Please suggest..

This is what i want to do to my request as shown in picture.

enter image description here

like image 786
Ranjana Dangol Avatar asked Jan 29 '16 11:01

Ranjana Dangol


People also ask

How do you pass body in GET request retrofit on Android?

This means your @GET or @DELETE should not have @Body parameter. You can use query type url or path type url or Query Map to fulfill your need. Else you can use other method annotation.

How do you post raw whole JSON in the body of a retrofit request?

The Best Answer is The @Body annotation defines a single request body. Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request. The Gson docs have much more on how object serialization works. And then use an instance of that class similar to #1.


1 Answers

Convert your data in object

public class Credentials
{
    public String email;
    public String password;
}

Set the data to object

Credentials loginCredentials = new Credentials();
loginCredentials.email = "[email protected]";
loginCredentials.password = "password";

Call your api

@POST("api/login")
Call<ApiResponse> loginUser(@Body Credentials credentials);
like image 138
Rohit5k2 Avatar answered Oct 20 '22 11:10

Rohit5k2