352

This question may have been asked before but no it was not definitively answered. How exactly does one post raw whole JSON inside the body of a Retrofit request?

See similar question here. Or is this answer correct that it must be form url encoded and passed as a field? I really hope not, as the services I am connecting to are just expecting raw JSON in the body of the post. They are not set up to look for a particular field for the JSON data.

I just want to clarify this with the restperts once and for all. One person answered not to use Retrofit. The other was not certain of the syntax. Another thinks yes it can be done but only if its form url-encoded and placed in a field (that's not acceptable in my case). No, I can't re-code all the services for my Android client. And yes, it's very common in major projects to post raw JSON instead of passing over JSON content as field property values. Let's get it right and move on. Can someone point to the documentation or example that shows how this is done? Or provide a valid reason why it can/should not be done.

UPDATE: One thing I can say with 100% certainty. You CAN do this in Google's Volley. It's built right in. Can we do this in Retrofit?

Community
  • 1
  • 1
user3243335
  • 3,521
  • 3
  • 12
  • 4

27 Answers27

517

The @Body annotation defines a single request body.

interface Foo {
  @POST("/jayson")
  FooResponse postJson(@Body FooRequest body);
}

Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request.

public class FooRequest {
  final String foo;
  final String bar;

  FooRequest(String foo, String bar) {
    this.foo = foo;
    this.bar = bar;
  }
}

Calling with:

FooResponse = foo.postJson(new FooRequest("kit", "kat"));

Will yield the following body:

{"foo":"kit","bar":"kat"}

The Gson docs have much more on how object serialization works.

Now, if you really really want to send "raw" JSON as the body yourself (but please use Gson for this!) you still can using TypedInput:

interface Foo {
  @POST("/jayson")
  FooResponse postRawJson(@Body TypedInput body);
}

TypedInput is a defined as "Binary data with an associated mime type.". There's two ways to easily send raw data with the above declaration:

  1. Use TypedByteArray to send raw bytes and the JSON mime type:

    String json = "{\"foo\":\"kit\",\"bar\":\"kat\"}";
    TypedInput in = new TypedByteArray("application/json", json.getBytes("UTF-8"));
    FooResponse response = foo.postRawJson(in);
    
  2. Subclass TypedString to create a TypedJsonString class:

    public class TypedJsonString extends TypedString {
      public TypedJsonString(String body) {
        super(body);
      }
    
      @Override public String mimeType() {
        return "application/json";
      }
    }
    

    And then use an instance of that class similar to #1.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • 5
    Very well, however, is there anyway to make this without making the pojos? – superUser Jul 25 '15 at 16:56
  • @Jake Wharton: how about using "JsonObject" from Gson? Does that work in a proper way? – Alireza Farahani Aug 23 '15 at 12:01
  • @Jake Wharton TypedInput has been removed in the new versions? How do we do a raw json post? – Hades Oct 19 '15 at 23:40
  • 31
    This is not working on retrofit 2. TypedInput and TypedString classes were removed. – Ahmed Hegazy Nov 27 '15 at 14:48
  • My problem is that the JSON content depends on the Object content. Let's say some fields on the JSON are optional, you include them depending on the field value. I don't know how to achieve this in Retrofit 2. – Sotti Nov 28 '15 at 17:22
  • 2
    @jakewharton What can we do for `TypedString` since it has been removed? – Jared Burrows Mar 16 '16 at 20:48
  • 14
    For Retrofit2, you can use `RequestBody` to create a raw body. – bnorm Apr 21 '16 at 13:09
  • 5
    I am getting this exception `java.lang.IllegalArgumentException: Unable to create @Body converter for class MatchAPIRequestBody (parameter #1)` – Shajeel Afzal Jan 24 '17 at 09:59
  • What should you annotate `foo` and `bar` with, to handle different name on the server? Also, is there a way to avoid the class, and have it all in the function `postJson` instead? Meaning that you could put the parameters as strings there directly, but just have it as json? I asked it here: https://stackoverflow.com/q/66256561/878126 – android developer Feb 18 '21 at 08:58
  • Don't forget to add @Serialized("foo")final String foo; @Serialized("bar") final String bar; otherwise you may get 400 error – Swapnil Kadam Dec 14 '21 at 19:33
188

Yes I know it's late, but somebody would probably benefit from this.

Using Retrofit2:

I came across this problem last night migrating from Volley to Retrofit2 (and as OP states, this was built right into Volley with JsonObjectRequest), and although Jake's answer is the correct one for Retrofit1.9, Retrofit2 doesn't have TypedString.

My case required sending a Map<String,Object> that could contain some null values, converted to a JSONObject (that won't fly with @FieldMap, neither does special chars, some get converted), so following @bnorms hint, and as stated by Square:

An object can be specified for use as an HTTP request body with the @Body annotation.

The object will also be converted using a converter specified on the Retrofit instance. If no converter is added, only RequestBody can be used.

So this is an option using RequestBody and ResponseBody:

In your interface use @Body with RequestBody

public interface ServiceApi
{
    @POST("prefix/user/{login}")
    Call<ResponseBody> login(@Path("login") String postfix, @Body RequestBody params);  
}

In your calling point create a RequestBody, stating it's MediaType, and using JSONObject to convert your Map to the proper format:

Map<String, Object> jsonParams = new ArrayMap<>();
//put something inside the map, could be null
jsonParams.put("code", some_code);

RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),(new JSONObject(jsonParams)).toString());
//serviceCaller is the interface initialized with retrofit.create...
Call<ResponseBody> response = serviceCaller.login("loginpostfix", body);
      
response.enqueue(new Callback<ResponseBody>()
    {
        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> rawResponse)
        {
            try
            {
             //get your response....
              Log.d(TAG, "RetroFit2.0 :RetroGetLogin: " + rawResponse.body().string());
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable throwable)
        {
        // other stuff...
        }
    });

An elegant Kotlin version of the above, to allow abstracting the parameters from the JSON convertion in the rest of your application code:

interface ServiceApi {

    @POST("/api/login")
    fun jsonLogin(@Body params: RequestBody): Deferred<LoginResult>

}

class ServiceApiUsingClass {

//ServiceApi init

    fun login(username: String, password: String) =
            serviceApi.jsonLogin(createJsonRequestBody(
                "username" to username, "password" to password))

    private fun createJsonRequestBody(vararg params: Pair<String, String>) =
            RequestBody.create(
                okhttp3.MediaType.parse("application/json; charset=utf-8"), 
                JSONObject(mapOf(*params)).toString())
}
starball
  • 20,030
  • 7
  • 43
  • 238
TommySM
  • 3,793
  • 3
  • 24
  • 36
  • 2
    Yeah I'm seeing a lot of complicated responses all over for this. If you're using Retrofit2 and want to do volley's `JsonObjectRequest`, all you need to do is this. Good answer. – Vic Vuci May 17 '16 at 20:02
  • 2
    Retrofit addes a key named "nameValuePairs" to the top of all the json objects. How can i remove this @TommySM – nr5 May 20 '16 at 10:10
  • 1
    Thank you! This solved my problem. Now I can send JSONObject directly without creating POJOs. – Erfan GLMPR Mar 28 '17 at 11:32
  • 1
    This is the only solution that helped me to `post a null value` to a property in the requestBody which otherwise was getting ignored. – Shubhral Apr 05 '17 at 11:12
  • I know i'm late but what is `jsonParams.put("code", some_code);` in the third line ? – Naveen Niraula Apr 26 '18 at 07:44
  • @NaveenNiraula you are correct, it is the map object two lines above - I edited my answer, thank you :) it is an example of how to add params to the JSON you are sending – TommySM Apr 26 '18 at 08:30
  • How can I put this request body object in Postman to test the service? – Darshan Popat Jul 26 '18 at 06:36
  • @TommySM @Shubhral @Erfan GLMPR the Kotlin one is not working. If I am using `Deferred` there is no enqueue method that I can invoke. I updated it to `Call` but its giving me IllegalArgumentException saying `HTTP method annotation is required (e.g., @GET, @POST, etc.).` Please help – hushed_voice Sep 16 '19 at 18:47
  • I wasn't able to get this Kotlin code working either, ran into the same problem free_style did. – szaske Oct 10 '19 at 00:04
  • 1
    Hey @hushed_voice I know I'm late for this but I ran into the same problem and solved it by moving the functions without annotations out of the interface. I edited the post for easier comprehension. – C.Schone Mar 24 '21 at 08:26
182

Instead of classes we can also directly use the HashMap<String, Object> to send body parameters for example

interface Foo {
  @POST("/jayson")
  FooResponse postJson(@Body HashMap<String, Object> body);
}
learner
  • 3,092
  • 2
  • 21
  • 33
  • What about if the json you we trying to send is kinda complex? like having arrays, and objects? – superUser Jul 25 '15 at 17:14
  • 2
    At that time you can create Hash map like HashMap ,it can be possible for creating kinda complex arrays and objects JSON. – learner Jul 27 '15 at 05:40
  • 6
    This is excellent if you do not want to be tied to a POJO of some kind. – Tim Apr 07 '16 at 10:04
  • Retrofit addes a key named "nameValuePairs" to the top of all the json objects. How can i remove this @Boopathi – nr5 May 20 '16 at 10:09
  • 2
    @Nil you cannot send json object by using retrofit...you adhere with pojo or my answer...this is nature of retrofit.if you want more about this ask Jake Wharton he is retrofit developer guy, his answer also available with pojo. – learner May 20 '16 at 18:39
  • @Nil By using Typed input you can achieve , there is conversion between json Typed input.Look about that. – learner May 20 '16 at 18:42
  • 8
    I get `IllegalArgumentException: Unable to create @Body converter for java.util.HashMap` with Moshi. I think Gson is needed for this to work – osrl Mar 02 '17 at 13:33
  • 1
    Seems like using `Map` solved the issue with Moshi – osrl Mar 02 '17 at 13:40
  • 5
    If using Kotlin use a hashmap of – peresisUser Jan 14 '19 at 15:05
  • 1
    Just ```val requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), YOUR_JSON_OBJECT.toString())``` – Sough Jun 25 '20 at 06:46
101

In Retrofit2, When you want to send your parameters in raw you must use Scalars.

first add this in your gradle:

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'

Your Interface

public interface ApiInterface {

    String URL_BASE = "http://10.157.102.22/rest/";

    @Headers("Content-Type: application/json")
    @POST("login")
    Call<User> getUser(@Body String body);

}

Activity

   public class SampleActivity extends AppCompatActivity implements Callback<User> {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiInterface.URL_BASE)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiInterface apiInterface = retrofit.create(ApiInterface.class);


        // prepare call in Retrofit 2.0
        try {
            JSONObject paramObject = new JSONObject();
            paramObject.put("email", "sample@gmail.com");
            paramObject.put("pass", "4384984938943");

            Call<User> userCall = apiInterface.getUser(paramObject.toString());
            userCall.enqueue(this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onResponse(Call<User> call, Response<User> response) {
    }

    @Override
    public void onFailure(Call<User> call, Throwable t) {
    }
}
  • 12
    The trick here is Scalar adapter before Gson, otherwise Gson will wrap your manually serialized JSON in a String. – TWiStErRob Jul 27 '17 at 15:31
  • 2
    [jonathan-nolasco-barrientos](https://stackoverflow.com/users/4103812/jonathan-nolasco-barrientos) you have to change .baseUrl(ApiInterface.ENDPOINT) to .baseUrl(ApiInterface.URL_BASE ) – Milad Ahmadi Nov 05 '17 at 09:07
  • 2
    When you use `GsonConverterFactory`, the `.toString()` is not necessary. You can declare `Call getUser(@Body JsonObject body);` using `JsonObject` instead of `JSONObject` and pass the `paramObject` directly. It will work just fine. – Igor de Lorenzi Mar 21 '18 at 04:00
  • This also works, but doesn't offer the freedom of dealing with your body in case it is not a String – Ishimwe Aubain Consolateur Sep 08 '18 at 11:16
  • This is the only solution that allows you to use JSON such as [{"var1":"x","var2":"y"}] – behelit Jan 07 '19 at 23:00
  • 1
    @IgordeLorenzi solve my issue, since I'm using spring boot to retrieve the json only JsonObject from gson works fine – haidarvm Jul 28 '19 at 06:29
  • 1
    @IgordeLorenzi Is there a difference which one is better JSONObject or JsonObject to use with scalars. – Sumit Shukla Jun 18 '20 at 05:47
  • 1
    @SumitShukla, as far as I know both must support the use of scalars well. If you want to see more opinions about some other differences: https://stackoverflow.com/questions/29042622/which-one-to-use-jsonobject-from-org-json-vs-jsonobject-from-javax-json – Igor de Lorenzi Jun 20 '20 at 16:24
51

Using JsonObject is the way it is:

  1. Create your interface like this:

    public interface laInterfaz{ 
        @POST("/bleh/blah/org")
        void registerPayer(@Body JsonObject bean, Callback<JsonObject> callback);
    }
    
  2. Make the JsonObject acording to the jsons structure.

    JsonObject obj = new JsonObject();
    JsonObject payerReg = new JsonObject();
    payerReg.addProperty("crc","aas22");
    payerReg.addProperty("payerDevManufacturer","Samsung");
    obj.add("payerReg",payerReg);
    /*json/*
        {"payerReg":{"crc":"aas22","payerDevManufacturer":"Samsung"}}
    /*json*/
    
  3. Call the service:

    service.registerPayer(obj, callBackRegistraPagador);
    
    Callback<JsonObject> callBackRegistraPagador = new Callback<JsonObject>(){
        public void success(JsonObject object, Response response){
            System.out.println(object.toString());
        }
    
        public void failure(RetrofitError retrofitError){
            System.out.println(retrofitError.toString());
        }
    };
    

And that its! In my personal opinion, its a lot better than making pojos and working with the class mess. This is a lot more cleaner.

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
superUser
  • 1,032
  • 12
  • 9
  • 1
    What if i dont want to send specif value in jsonobject class. which annotaion can i use above veriable for that? – aligur Jan 04 '16 at 15:15
  • 1
    As you can see the above example... JsonObject as it is an object, does not use any anotation. In your case if you dont want to send specific value, you might just not add it as a property... – superUser Jan 08 '16 at 14:39
  • 1
    I mean i dont want to send value which is declared in the class. Btw i fixed the problem. There was a annotation for that which name is expose. – aligur Jan 09 '16 at 02:34
  • 2
    This is the most flexible way. You can construct your json object even if you don't know how many fields you will have or even if you don't know they names +1 from me – Stoycho Andreev May 30 '16 at 13:42
  • 1
    i m getting error Service methods cannot return void. for method APIServices.signUpUser – Erum Aug 23 '16 at 06:15
  • This is sure to work guys and also, you don't need to create all those model classes for requests and reponses. meaning more flexibility – Neo Jul 15 '22 at 17:52
13

Add ScalarsConverterFactory to retrofit:

in gradle:

implementation'com.squareup.retrofit2:converter-scalars:2.5.0'

your retrofit:

retrofit = new Retrofit.Builder()
            .baseUrl(WEB_DOMAIN_MAIN)
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

change your call interface @Body parameter to String, don't forget to add @Headers("Content-Type: application/json"):

@Headers("Content-Type: application/json")
@POST("/api/getUsers")
Call<List<Users>> getUsers(@Body String rawJsonString);

now you can post raw json.

ali-star
  • 706
  • 1
  • 10
  • 19
12

I particularly like Jake's suggestion of the TypedString subclass above. You could indeed create a variety of subclasses based on the sorts of POST data you plan to push up, each with its own custom set of consistent tweaks.

You also have the option of adding a header annotation to your JSON POST methods in your Retrofit API…

@Headers( "Content-Type: application/json" )
@POST("/json/foo/bar/")
Response fubar( @Body TypedString sJsonBody ) ;

…but using a subclass is more obviously self-documenting.

@POST("/json/foo/bar")
Response fubar( @Body TypedJsonString jsonBody ) ;
Community
  • 1
  • 1
zerobandwidth
  • 1,213
  • 11
  • 18
11

1)Add dependencies-

 compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

2) make Api Handler class

    public class ApiHandler {


  public static final String BASE_URL = "URL";  

    private static Webservices apiService;

    public static Webservices getApiService() {

        if (apiService == null) {

           Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();
            Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(gson)).baseUrl(BASE_URL).build();

            apiService = retrofit.create(Webservices.class);
            return apiService;
        } else {
            return apiService;
        }
    }


}

3)make bean classes from Json schema 2 pojo

Remember
-Target language : Java -Source type : JSON -Annotation style : Gson -select Include getters and setters -also you may select Allow additional properties

http://www.jsonschema2pojo.org/

4)make interface fro api calling

    public interface Webservices {

@POST("ApiUrlpath")
    Call<ResponseBean> ApiName(@Body JsonObject jsonBody);

}

if you have a form-data parameters then add below line

@Headers("Content-Type: application/x-www-form-urlencoded")

Other way for form-data parameter check this link

5)make JsonObject for passing in to body as parameter

 private JsonObject ApiJsonMap() {

    JsonObject gsonObject = new JsonObject();
    try {
        JSONObject jsonObj_ = new JSONObject();
        jsonObj_.put("key", "value");
        jsonObj_.put("key", "value");
        jsonObj_.put("key", "value");


        JsonParser jsonParser = new JsonParser();
        gsonObject = (JsonObject) jsonParser.parse(jsonObj_.toString());

        //print parameter
        Log.e("MY gson.JSON:  ", "AS PARAMETER  " + gsonObject);

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

    return gsonObject;
}

6) Call Api Like this

private void ApiCallMethod() {
    try {
        if (CommonUtils.isConnectingToInternet(MyActivity.this)) {
            final ProgressDialog dialog;
            dialog = new ProgressDialog(MyActivity.this);
            dialog.setMessage("Loading...");
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();

            Call<ResponseBean> registerCall = ApiHandler.getApiService().ApiName(ApiJsonMap());
            registerCall.enqueue(new retrofit2.Callback<ResponseBean>() {
                @Override
                public void onResponse(Call<ResponseBean> registerCall, retrofit2.Response<ResponseBean> response) {

                    try {
                        //print respone
                        Log.e(" Full json gson => ", new Gson().toJson(response));
                        JSONObject jsonObj = new JSONObject(new Gson().toJson(response).toString());
                        Log.e(" responce => ", jsonObj.getJSONObject("body").toString());

                        if (response.isSuccessful()) {

                            dialog.dismiss();
                            int success = response.body().getSuccess();
                            if (success == 1) {



                            } else if (success == 0) {



                            }  
                        } else {
                            dialog.dismiss();


                        }


                    } catch (Exception e) {
                        e.printStackTrace();
                        try {
                            Log.e("Tag", "error=" + e.toString());

                            dialog.dismiss();
                        } catch (Resources.NotFoundException e1) {
                            e1.printStackTrace();
                        }

                    }
                }

                @Override
                public void onFailure(Call<ResponseBean> call, Throwable t) {
                    try {
                        Log.e("Tag", "error" + t.toString());

                        dialog.dismiss();
                    } catch (Resources.NotFoundException e) {
                        e.printStackTrace();
                    }
                }

            });

        } else {
            Log.e("Tag", "error= Alert no internet");


        }
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
    }
}
Adil
  • 812
  • 1
  • 9
  • 29
10

I found that when you use a compound object as @Body params, it could not work well with the Retrofit's GSONConverter (under the assumption you are using that). You have to use JsonObject and not JSONObject when working with that, it adds NameValueParams without being verbose about it - you can only see that if you add another dependency of logging interceptor, and other shenanigans.

So what I found the best approach to tackle this is using RequestBody. You turn your object to RequestBody with a simple api call and launch it. In my case I'm converting a map:

   val map = HashMap<String, Any>()
        map["orderType"] = orderType
        map["optionType"] = optionType
        map["baseAmount"] = baseAmount.toString()
        map["openSpotRate"] = openSpotRate.toString()
        map["premiumAmount"] = premiumAmount.toString()
        map["premiumAmountAbc"] = premiumAmountAbc.toString()
        map["conversionSpotRate"] = (premiumAmountAbc / premiumAmount).toString()
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSONObject(map).toString())

and this is the call:

 @POST("openUsvDeal")
fun openUsvDeal(
        @Body params: RequestBody,
        @Query("timestamp") timeStamp: Long,
        @Query("appid") appid: String = Constants.APP_ID,
): Call<JsonObject>
peresisUser
  • 1,680
  • 18
  • 23
8

This is what works me for the current version of retrofit 2.6.2,

First of all, we need to add a Scalars Converter to the list of our Gradle dependencies, which would take care of converting java.lang.String objects to text/plain request bodies,

implementation'com.squareup.retrofit2:converter-scalars:2.6.2'

Then, we need to pass a converter factory to our Retrofit builder. It will later tell Retrofit how to convert the @Body parameter passed to the service.

private val retrofitBuilder: Retrofit.Builder by lazy {
    Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
}

Note: In my retrofit builder i have two converters Gson and Scalars you can use both of them but to send Json body we need to focus Scalars so if you don't need Gson remove it

Then Retrofit service with a String body parameter.

@Headers("Content-Type: application/json")
@POST("users")
fun saveUser(@Body   user: String): Response<MyResponse>

Then create the JSON body

val user = JsonObject()
 user.addProperty("id", 001)
 user.addProperty("name", "Name")

Call your service

RetrofitService.myApi.saveUser(user.toString())
Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33
7

You can use hashmap if you don't want to create pojo class for every API call.

HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("email","this@gmail.com");
        hashMap.put("password","1234");

And then send like this

Call<JsonElement> register(@Body HashMap registerApiPayload);
jatin rana
  • 825
  • 1
  • 9
  • 21
5

After so much effort, found that the basic difference is you need to send the JsonObject instead of JSONObject as parameter.

umair151
  • 553
  • 5
  • 21
5

use following to send json

final JSONObject jsonBody = new JSONObject();
    try {

        jsonBody.put("key", "value");

    } catch (JSONException e){
        e.printStackTrace();
    }
    RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),(jsonBody).toString());

and pass it to url

@Body RequestBody key
Mahesh Pandit
  • 348
  • 3
  • 7
5

If you don't want to create extra classes or use JSONObject you can use a HashMap.

Retrofit interface:

@POST("/rest/registration/register")
fun signUp(@Body params: HashMap<String, String>): Call<ResponseBody>

Call:

val map = hashMapOf(
    "username" to username,
    "password" to password,
    "firstName" to firstName,
    "surname" to lastName
)

retrofit.create(TheApi::class.java)
     .signUp(map)
     .enqueue(callback)
SoftDesigner
  • 5,640
  • 3
  • 58
  • 47
5

Things required to send raw json in Retrofit.

1) Make sure to add the following header and remove any other duplicate header. Since, on Retrofit's official documentation they specifically mention-

Note that headers do not overwrite each other. All headers with the same name will be included in the request.

@Headers({"Content-Type: application/json"})

2) a. If you are using a converter factory you can pass your json as a String, JSONObject, JsonObject and even a POJO. Also have checked, having ScalarConverterFactory is not necessary only GsonConverterFactory does the job.

@POST("/urlPath")
@FormUrlEncoded
Call<Response> myApi(@Header("Authorization") String auth, @Header("KEY") String key, 
                     @Body JsonObject/POJO/String requestBody);

2) b. If you are NOT using any converter factory then you MUST use okhttp3's RequestBody as Retrofit's documentation says-

The object will also be converted using a converter specified on the Retrofit instance. If no converter is added, only RequestBody can be used.

RequestBody requestBody=RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);

@POST("/urlPath")
@FormUrlEncoded
Call<Response> myApi(@Header("Authorization") String auth, @Header("KEY") String key, 
                 @Body RequestBody requestBody);

3) Success!!

Darshan Miskin
  • 844
  • 14
  • 25
4

Based on the top answer, I have a solution to not have to make POJOs for every request.

Example, I want to post this JSON.

{
    "data" : {
        "mobile" : "qwer",
        "password" : "qwer"
    },
    "commom" : {}
}

then, I create a common class like this:

import java.util.Map;
import java.util.HashMap;

public class WRequest {

    Map<String, Object> data;
    Map<String, Object> common;

    public WRequest() {
        data = new HashMap<>();
        common = new HashMap<>();
    }
}

Finally, when I need a json

WRequest request = new WRequest();
request.data.put("type", type);
request.data.put("page", page);

The request marked annotation @Body then can pass to Retrofit.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
wjploop
  • 209
  • 2
  • 8
4

For more clarity on the answers given here, this is how you can use the extension functions. This is only if you are using Kotlin

If you are using com.squareup.okhttp3:okhttp:4.0.1 the older methods of creating objects of MediaType and RequestBody have been deprecated and cannot be used in Kotlin.

If you want to use the extension functions to get a MediaType object and a ResponseBody object from your strings, firstly add the following lines to the class in which you expect to use them.

import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody

You can now directly get an object of MediaType this way

val mediaType = "application/json; charset=utf-8".toMediaType()

To get an object of RequestBody first convert the JSONObject you want to send to a string this way. You have to pass the mediaType object to it.

val requestBody = myJSONObject.toString().toRequestBody(mediaType)
Devenom
  • 915
  • 1
  • 9
  • 20
3

you need to set @Body in interface

@Headers({ "Content-Type: application/json;charset=UTF-8"})
    @POST("Auth/Login")
    Call<ApiResponse> loginWithPhone(@Body HashMap<String, String> fields);

To pass the raw body to retrofit just use:

 HashMap<String,String> SendData =new HashMap<>();
        SendData.put("countryCode",ccode);
        SendData.put("phoneNumber",phone);

        Call<ApiResponse>call = serviceInterface.loginWithPhone(SendData);

this works for me:

Khaliq Izrail
  • 89
  • 2
  • 10
2

Solved my problem based on TommySM answer (see previous). But I didn't need to make login, I used Retrofit2 for testing https GraphQL API like this:

  1. Defined my BaseResponse class with the help of json annotations (import jackson.annotation.JsonProperty).

    public class MyRequest {
        @JsonProperty("query")
        private String query;
    
        @JsonProperty("operationName")
        private String operationName;
    
        @JsonProperty("variables")
        private String variables;
    
        public void setQuery(String query) {
            this.query = query;
        }
    
        public void setOperationName(String operationName) {
            this.operationName = operationName;
        }
    
        public void setVariables(String variables) {
            this.variables = variables;
        }
    }
    
  2. Defined the call procedure in the interface:

    @POST("/api/apiname")
    Call<BaseResponse> apicall(@Body RequestBody params);
    
  3. Called apicall in the body of test: Create a variable of MyRequest type (for example "myLittleRequest").

    Map<String, Object> jsonParams = convertObjectToMap(myLittleRequest);
    RequestBody body = 
         RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                        (new JSONObject(jsonParams)).toString());
    response = hereIsYourInterfaceName().apicall(body).execute();
    
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
2

I wanted to compare speed of volley and retrofit for sending and receiving data I wrote below code (for retrofit part)

first dependency:

dependencies {
     implementation 'com.squareup.retrofit2:retrofit:2.4.0'
     implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
}

Then interface:

 public interface IHttpRequest {

    String BaseUrl="https://example.com/api/";

    @POST("NewContract")
    Call<JsonElement> register(@Body HashMap registerApiPayload);
}

and a function to set parameters to post data to server(In MainActivity):

private void Retrofit(){

    Retrofit retrofitRequest = new Retrofit.Builder()
            .baseUrl(IHttpRequest.BaseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    // set data to send
    HashMap<String,String> SendData =new HashMap<>();
    SendData.put("token","XYXIUNJHJHJHGJHGJHGRTYTRY");
    SendData.put("contract_type","0");
    SendData.put("StopLess","37000");
    SendData.put("StopProfit","48000");

    final IHttpRequest request=retrofitRequest.create(IHttpRequest.class);

    request.register(SendData).enqueue(new Callback<JsonElement>() {
        @Override
        public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
            if (response.isSuccessful()){
                Toast.makeText(getApplicationContext(),response.body().toString(),Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<JsonElement> call, Throwable t) {

        }
    });

}

And I found Retrofit faster than volley in my case.

Shojaeddin
  • 1,851
  • 1
  • 18
  • 16
2

API Call

@Headers("Content-Type: application/json")
@POST("/set_data")
Call<CommonResponse> setPreferences(@Body RequestData request);

Note: Use GSON library of Retrofit

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class RequestData {

    @SerializedName("access_token")
    @Expose
    private String accessToken;

    @SerializedName("data")
    @Expose
    private Data data;
    // The above 'Data' is another similar class to add inner JSON objects. JSONObject within a JSONObject.

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public void setData(Data data) {
        this.data = data;
    }
}

I guess that will help, rest all integration you might already have had and we don't need anything fancy to use above code snippet. It's working perfectly for me.

Harpreet
  • 2,990
  • 3
  • 38
  • 52
2

Updated solution for 2022:

One of the first things to check is that your post request is working via a third party API such as postman. I had done this before coming across the solutions on this page.

The next step is to add logging facilities to your retrofit instance. Click here on how to add logging to retrofit.

Upon adding logging I saw a 500 server error, based on the fact that the end-point was working via Postman we know that the error must be something to do with the format of the data that is passed to the Post method.

Your retrofit builder should look like this:

val retrofitInstance = Retrofit.Builder()
            .baseUrl("https://pacific-tundra-61285.herokuapp.com/")
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient)
            .build()

This post helped a lot in helping solve this problem and provided the correct way to convert the object into the correct "application/json" format when making the post request. There were a few deprecated methods used in the kotlin version, the new code is very similar:

private fun createRequestBody(vararg params : Pair<String, Any>) =
        JSONObject(mapOf(*params)).toString()
            .toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())

The generic value parameter in the pair is set to Any so that you can handle the different types related to your object.

The final piece just for clarity is the actual post method and the code that is used to invoke the post request.

@POST("create/")
    fun create(@Body params : RequestBody) : Call<YourObject>
val call = apiService.create(createRequestBody( 
"string" to object // You should pass in any key and value pairs here.

Finally call enqueue on the call as usual.

SibMo
  • 68
  • 1
  • 6
1

I tried this: When you are creating your Retrofit instance, add this converter factory to the retrofit builder:

gsonBuilder = new GsonBuilder().serializeNulls()     
your_retrofit_instance = Retrofit.Builder().addConverterFactory( GsonConverterFactory.create( gsonBuilder.create() ) )
1

While creating OkHttpClient that will be used for Retrofit.

add an Interceptor like this.

 private val httpClient = OkHttpClient.Builder()
        .addInterceptor (other interceptors)
        ........................................

        //This Interceptor is the main logging Interceptor
        .addInterceptor { chain ->
            val request = chain.request()
            val jsonObj = JSONObject(Gson().toJson(request))

            val requestBody = (jsonObj
            ?.getJSONObject("tags")
            ?.getJSONObject("class retrofit2.Invocation")
            ?.getJSONArray("arguments")?.get(0) ?: "").toString()
            val url = jsonObj?.getJSONObject("url")?.getString("url") ?: ""
            
            Timber.d("gsonrequest request url: $url")
            Timber.d("gsonrequest body :$requestBody")

            chain.proceed(request)
        }
        
        ..............
        // Add other configurations
        .build()

Now your every Retrofit call's URL and request body will be logged in Logcat. Filter it by "gsonrequest"

Houman
  • 64,245
  • 87
  • 278
  • 460
erluxman
  • 18,155
  • 20
  • 92
  • 126
1

enter image description here

Add ScalarsConverterFactory.create() method and pass hard code

R M Vivek
  • 44
  • 3
0

JSONObject showing error please use

JsonObject paramObject = new JsonObject(); paramObject.addProperty("loginId", vMobile_Email);

0
@Headers(value = "Content-Type: application/json")
@POST("api/Persona/Add")
Call<Persona> AddPersona(@Header("authorization") String token, @Body JsonObject object);
 JsonObject postParam = new JsonObject();
       postParam.addProperty("PersonaCedula", item.getPersonaCedula());
KiluSs
  • 421
  • 4
  • 13