0

I have a POJO that contains a single authentication token in a string variable. I need to send this to my API using the following template:

{ “auth” : { api_key”:”XXXX” } }

I am using moshi to convert my POJOs to JSON, which returns

{"api-key":"XXXX"}

How can I add the extra brackets on the outside of my current one using a retrofit converter?

2 Answers2

0

I think you should use oAuth authentication. Then, you can use an interceptor for Retrofit and populate that token.

maxwellnewage
  • 355
  • 2
  • 13
0

You can use Moshi-Lazy-Adapters to wrap the json. Just use the following annotation when you send the body:

@POST("your_post_url") 
Call<YourResponseTypeHere> sendApiKey(@Body @Wrapped({"auth", "api_key"}) String apiKey);

The library's adapter will do the rest. And you don't need to declare an extra object.

Serj Lotutovici
  • 4,370
  • 5
  • 32
  • 41