Word Associations

부분 유료
Verified
분류별 Twinword API | 업데이트됨 17 дней назад | Text Analysis
인기

9.6 / 10

지연 시간

496ms

서비스 수준

100%

Health Check

N/A

모든 토론으로 돌아가기

okhttp does not work

Rapid account: Chrishpowell
chrishpowell
7 месяцев назад

I have Java code as follows:

    /* okhttp3, Does not work */
    // Note: correct order for RequestBody v4.x.x
    RequestBody rb = RequestBody.create( "entry=sound", MediaType.get("application/x-www-form-urlencoded") );
    Request req = new Request.Builder()
                .url("https://twinword-word-associations-v1.p.rapidapi.com/associations/")
                .post(rb)
                .addHeader("content-type", "application/x-www-form-urlencoded")
                .addHeader("X-RapidAPI-Key", "<My-long-key>")
                .addHeader("X-RapidAPI-Host", "twinword-word-associations-v1.p.rapidapi.com")
                .build();

    try( Response resp = client.newCall(req).execute() )
    {
        // Returns: {"result_code":"711","result_msg":"Quota exceeded or invalid access"}
        System.out.println(resp.body().string());
    }
    
    /* java.net.http, Works */
    java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder()
                .uri(URI.create("https://twinword-word-associations-v1.p.rapidapi.com/associations/"))
                .header("content-type", "application/x-www-form-urlencoded")
                .header("X-RapidAPI-Key", "<My-long-key>")
                .header("X-RapidAPI-Host", "twinword-word-associations-v1.p.rapidapi.com")
                .method("POST", java.net.http.HttpRequest.BodyPublishers.ofString("entry=sound"))
                .build();
    
    HttpResponse<String> response = HttpClient.newHttpClient()
                .send(request, HttpResponse.BodyHandlers.ofString());
    
    // Returns: {"entry":"sound" ... "result_msg":"Success"}
     System.out.println(response.body());

I am using okttp3 4.11.0 rather than the seemingly old version implied in the example code. I would prefer to use okhttp3 as it’s used all over other parts of code and is supposedly faster. How can I get okhttp to work?

Rapid account: Twinword
twinword Commented 7 месяцев назад

Great! Thank you for the update.

Rapid account: Chrishpowell
chrishpowell Commented 7 месяцев назад

Thanks for your patience. The code you refer to is Kotlin, btw, and the assertion that MediaType.get() is no longer available is incorrect - at least in Java.

I have solved the problem. The following code works:

    OkHttpClient client = new OkHttpClient();
    
    // For posting form parameters use this:
    RequestBody rb = new FormBody.Builder()
                .add("entry","sound")
                .build();
            
    Request req = new Request.Builder()
                .url("https://twinword-word-associations-v1.p.rapidapi.com/associations/")
                .addHeader("X-RapidAPI-Key", AUTH_KEY)
                .addHeader("X-RapidAPI-Host", "twinword-word-associations-v1.p.rapidapi.com")
                .post(rb)
                .build();

    try (Response resp = client.newCall(req).execute())
    {
        System.out.println(resp.body().string());
    }

I will point this out to RapidAPI.

Many thanks.

Rapid account: Twinword
twinword Commented 7 месяцев назад

It seems there is a misunderstanding with RapidAPI because API providers cannot modify the code snippets. If you visit other APIs on RapidAPI, you will see that all has the same code snippets except different endpoints. That’s why new version of okhttp code snippet should be coined by RapidAPI. In fact, RapidAPI has a role as a connector by providing those code snippets. Once we provide them JSON Restful API, then RapidAPI helps us to sell and consume our endpoints in many languages.

What I can do is to ask RapidAPI to add new version of okhttp on behalf of you. Meanwhile, can you test if this issue exists with Twinword API only or not? In other words, Twinword API is the special case that do not work with okhttp3 v4.x.x on RapidAPI from your test while others are okay?

Other suggestion is that, can you check the second to top answer from Skack Overflow (https://stackoverflow.com/questions/57100451/okhttp3-requestbody-createcontenttype-content-deprecated)?

It says MediaType.get() is not available anymore as the below. I think it’s worth trying.


In com.squareup.okhttp3:okhttp:4.1.0
MediaType.get(“application/json; charset=utf-8”) no more available.
instead this we need to use “application/json; charset=utf-8”.toMediaTypeOrNull().

Rapid account: Chrishpowell
chrishpowell Commented 7 месяцев назад

Sirs

I contacted RapidAPI and, as they point out, they are merely a market place connecting consumers such as me with API providers such as you. They do not help with writing or embedding code. Consequently, you should look into updating your API such that it works with okhttp3 v4.x.x and not rely on unsupported okhttp3 v 3.x.x.

I look forward to your reply and update.

Rapid account: Chrishpowell
chrishpowell Commented 7 месяцев назад

“…since the code snippets are managed by RapidAPI.”

Ah! Ok, thanks, I will ask RapidAPI.

Rapid account: Twinword
twinword Commented 7 месяцев назад

Sorry for wasting your time. I have to admit that I was not helpful on this topic.

The result code, 711 means our endpoint got no proper values including X-RapidAPI-Key if quota is not a problem. In this case, what API provider can do is very limited. To work around this, I’d like to suggest using Stack Overflow where more developers might help or request new version at https://support.rapidapi.com/hc/en-us since the code snippets are managed by RapidAPI.

Rapid account: Chrishpowell
chrishpowell Commented 7 месяцев назад

Yes - your code example is deprecated and the top answer in that Stackoverflow is:

Java Solution: Use create(String, MediaType) instead of create(MediaType, String).

Which, as you can see, is what I have used in my example code. But your system rejects this with result code 711. Are you going to address this please?

Rapid account: Twinword
twinword Commented 7 месяцев назад

I’ve found this regarding “Okhttp3 - RequestBody.create(contentType, content) Deprecated”.

https://stackoverflow.com/questions/57100451/okhttp3-requestbody-createcontenttype-content-deprecated

I hope this helps you.

Rapid account: Chrishpowell
chrishpowell Commented 7 месяцев назад

Please note this line:

RequestBody body = RequestBody.create(mediaType, “entry=sound”);

is deprecated. The order in newer versions of okhttp has changed - as I pointed out in a comment in the code.

Rapid account: Twinword
twinword Commented 7 месяцев назад

How about trying the below from the RapidAPI code snippets?

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse(“application/x-www-form-urlencoded”);
RequestBody body = RequestBody.create(mediaType, “entry=sound”);
Request request = new Request.Builder()
.url(“https://twinword-word-associations-v1.p.rapidapi.com/associations/”)
.post(body)
.addHeader(“X-RapidAPI-Key”, “<My-long-key>”)
.addHeader(“X-RapidAPI-Host”, “twinword-word-associations-v1.p.rapidapi.com”)
.addHeader(“Content-Type”, “application/x-www-form-urlencoded”)
.build();

Response response = client.newCall(request).execute();

아래에 의견을 추가하고 토론에 참여하세요.

새 댓글을 게시하려면 로그인 / 가입