0

I send to server using @FormUrlEncoded. When I send strings to server, I get the requestbody like this

    Content-Length: 14
    Authorization: 
    nick=%E3%85%8A

So I treid addInterceptor. interceptor

        val requestBuilder = original.newBuilder()
            .addHeader("Authorization",token)
            .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
            .url(originalHttpUrl)

ApiInterface

    @FormUrlEncoded
    @POST("nickCheck")
    fun checkNickName(
       @Fiel("nick") nick:  String
    ) :Call<NickCheckResultData>

But It didn't work. english and numbers are ok, but not my language. how to fix it? Also I tried settings->file encoding -> change utf-8

Charles
  • 181
  • 1
  • 14

1 Answers1

0

There is no problem with UTF-8.

The annotation @FormUrlEncoded means that what you are sending will be URL encoded and this is what happens here.

When a string is url encoded it will only keep unchanged, characters that are valid in a url, like you are observing "english and numbers are ok".

As you can read in the Android documentation here : "The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same." Because they are valid characters in a URL.

But Korean characters are not valid for an url, so they are encoded (transformed), and this is what happens here.

What you have to do in you server is to then decode the received encoded string. For example in PHP : urldecode("%E3%85%8A") will result in your original string : "ㅊ"

You can also remove the annotation @FormUrlEncoded or send your data using an HTTP request body with the @Body annotation. Check out the retrofit documentation for more info on how to do it.

kppro
  • 879
  • 5
  • 5
  • thank you for your answer. so if I wanna send my korean language, you mean there is no way to send literal character and server has to decode this? – Charles May 18 '20 at 06:42
  • Yes you can, just remove the @FormUrlEncoded annotation, and it will not encode what you are sending – kppro May 19 '20 at 10:35