1

I'm working on refactoring my university's mobile applications API.

The main idea was to move it from Jsoup to Retrofit because of the better classes structure (and because Google recommends so).

I found that previous version was built around this call:

Jsoup.connect(url)
     .data(request.getArgs())
     .ignoreContentType(true)
     .method(Connection.Method.POST)
     .timeout(timeout)
     .execute();

Where .data() args is Map<Key, Value>, which is transforming into a query like url?key1=value1&key2=vaule2&...&keyN=valueN as followed in the Jsoup docs.

I have tested this call, and it works properly – returning a JSON object.

So with this info I was trying to recreate such behavior using Retrofit2 API. I have created a service interface:

public interface MyService {
    @POST("url")
    Call<ResponseBody> myRequest(@Query("key1") String value1, @Query("key2") String value2);
}

But when I tried to call this request, as the response body I got a 404 HTML-page instead of the expected JSON.

So here is my answer: is there any difference between how these two solutions forming their requests? I was trying to go deeper, but found nothing.

vo1d
  • 11
  • 3

2 Answers2

0

Well, JSoup is an HTML parser while Retrofit is an HTTP client, so right off the bat it would seem you're replacing a tool that serves one purpose with another that serves a completely different purpose.

The best way to see what each call is doing is to either a) read the documentation for each library to understand what each call in the chains are doing or, b) use a tool like Charles Proxy, Proxyman, or Fiddler to intercept the calls done with each library and compare the parameters of the requests each one makes (query parameters, headers, post body, etc).

dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • Thanks! It's kinda easy to intercept connection parameters for Retrofit, but Jsoup is not so public. – vo1d Apr 24 '21 at 20:56
  • The only thing I'd like to say that in this project Jsoup doesn't parse any html files. It's used only for sending requests. And also there is no additional settings except mentioned code line. So that's why it's a puzzle for me. – vo1d Apr 24 '21 at 21:00
  • "Jsoup is not so public" - not sure what you mean, but again, using any of the tools I listed you should be able to see the raw HTTP request that is gong out over the network when making the Jsoup call. How "public" Jsoup is should not matter. – dominicoder Apr 24 '21 at 22:00
  • "And also there is no additional settings except mentioned code line." - which means there's no telling what defaults JSoup is adding or not adding to the request before it goes out. Again - inspect the raw HTTP(s) request that's actually going out for each call. – dominicoder Apr 24 '21 at 22:02
0

I just needed to read Jsoup documentation more attentively. There is a note in the description of one of the data() method overloads: for POST requests all the data is going to request body. So with Retrofit2 I just need to use @FieldMap attribute instead of a @Query.

vo1d
  • 11
  • 3