How to POST raw whole JSON in the body of a Retrofit request?

前端 未结 23 2724
面向向阳花
面向向阳花 2020-11-22 00:57

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

23条回答
  •  渐次进展
    2020-11-22 01:46

    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> getUsers(@Body String rawJsonString);
    

    now you can post raw json.

提交回复
热议问题