3

I'm using Postman version 8.5.1 and I'm trying to use the code OkHTTP code snippets. It seems some statements are not working like ...

OkHttpClient client = new OkHttpClient().newBuilder().build();

However this seems to work ...

OkHttpClient client = new OkHttpClient();

Also this doesn't work ...

  .addFormDataPart("file","test.jpg",RequestBody.create(MediaType.parse("application/octet-stream"),new File("/Users/tm/Desktop/test.jpg")))

It mentions something about a wrong RequestBody object ...

I'm using this maven repo ...

    <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>okhttp</artifactId>
        <version>2.7.5</version>
    </dependency>

This is the import statement

 import com.squareup.okhttp.*;

What version of the okhttp is fully compatible with the Postman code snippets? Which maven repo should be used?

It seems there is also a okhttp3 ...

user10053673
  • 221
  • 1
  • 13
  • Why are you bringing up postman? What does this have to do with your problem using the okhttp library? Show us a more complete example. – Deadron Jun 01 '21 at 21:34

2 Answers2

5

It seems the code snippets are okhttp3 and not okhttp...

This seems to work ...

    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.1</version>
    </dependency>

import statement

 import okhttp3.*;
user10053673
  • 221
  • 1
  • 13
  • Also, we would get the error "MultipartBuilder.Builder cannot be resolved to a type".If we have below line new MultipartBuilder.Builder().setType(MultipartBody.FORM) And the fix would be to replace MultipartBuilder with MultipartBody as given below. RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM) Reference: https://stackoverflow.com/questions/34676044/multipartbuilder-cant-be-resolved-in-okhttp3-0-0-rc1 – aksankar Apr 05 '22 at 11:03
0

How are you using the snippets from Postman, where are you pasting these ? Please post a whole snippet of your code. In general get the latest version of okhttp --> https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp/4.2.2.

Bender
  • 72
  • 6