0

I have this request and I need to send it by FormUrlEncoded using Retrofit

{
   "clnt_id": "OQW",
   "clnt_res": "AA!@#$T",
   "type": "SCDS",
   "len": "ASD"
}

I used this code:

    @FormUrlEncoded
    @POST("Endpoint")
    @Headers("Accept: Application/JSON")
    fun connect(
        @Field("clnt_id") clnt_id: String,
        @Field(value = "clnt_res", encoded = false) clnt_res: String,
        @Field("type") type: String,
        @Field("len") len: String
    ): Observable<Token>

First, thing is that the request is not sent as JSON

Second, the value of "clnt_res", encoded by retrofit

A. Albrg
  • 485
  • 4
  • 21

2 Answers2

0

You have 2 options to send json request from android using Retrofit.

  1. Create Pojo Model of json request and pass it by setting values of it.
  2. Create HashMap and pass it to request.

Here is solution using 2nd Method:

Create hashmap and put key(parameters) and value:

Map<String,String> requestMap = new HashMap<>();
        requestMap.put("clnt_id","your_value");
        requestMap.put("clnt_res","your_value");
        requestMap.put("type","your_value");
        requestMap.put("len","your_value");

Then pass it to your retrofit request using FieldMap:

@FormUrlEncoded
    @POST("Endpoint")
    @Headers("Accept: Application/JSON")
    fun connect(
        @FieldMap requestMap:Map<String,String>
    ): Observable<Token>
Vishal Thakkar
  • 652
  • 4
  • 18
  • I try second method, but nothing change, it is like my code no difference just you created a Map instead of Fields And the value of "clnt_res" is still encoded by retrofit – A. Albrg Nov 12 '19 at 09:52
  • is it working in postman?? can you please show me how do you call in postman?? – Vishal Thakkar Nov 12 '19 at 10:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202198/discussion-between-vishal-thakkar-and-a-albrg). – Vishal Thakkar Nov 12 '19 at 10:19
0

I finally get the answer and it was a problem with symbol '$' in 'clnt_res' value "AA!@#$T", The problem is in kotlin to escape a special char you need to do this "\$", what I made the IDE didn't tell me that it is wrong is this "$/".

halfer
  • 19,824
  • 17
  • 99
  • 186
A. Albrg
  • 485
  • 4
  • 21
  • Still not Resolved in Java, I am coping value from a TextView. If anyone enters '@' character, it gets converted to '%40' – SAM Aug 18 '21 at 12:00