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

前端 未结 23 2386
面向向阳花
面向向阳花 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:35

    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): Call
    

    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)
    

提交回复
热议问题