Search in sources :

Example 91 with RequestBody

use of okhttp3.RequestBody in project chefly_android by chef-ly.

the class HttpConnection method downloadFromFeed.

public String downloadFromFeed(RequestMethod requestPackage) throws IOException {
    String address = requestPackage.getEndpoint();
    String encodedParams = requestPackage.getEncodedParams();
    //check for get request
    if (requestPackage.getMethod().equals("GET") && encodedParams.length() > 0) {
        address = String.format("%s?%s", address, encodedParams);
    }
    //create request object
    OkHttpClient client;
    if (BuildConfig.DEBUG) {
        client = new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor()).build();
    } else {
        client = new OkHttpClient();
    }
    Request.Builder requestBuilder = new Request.Builder().url(address);
    //  Add required HTTP Header
    Map<String, String> headers = requestPackage.getHeaders();
    if (headers.size() > 0) {
        for (String k : headers.keySet()) {
            if (k != null) {
                requestBuilder.addHeader(k, headers.get(k));
            }
        }
    }
    //check for post request
    if (requestPackage.getMethod().equals("POST")) {
        //extract the parameters from the request
        //get the reference of the pair
        Map<String, String> params = requestPackage.getParams();
        String msg = "";
        if (params.size() == 1) {
            for (String key : params.keySet()) msg = params.get(key);
        } else {
            JSONObject parameter = new JSONObject(params);
            msg = parameter.toString();
        }
        RequestBody body = RequestBody.create(JSON, msg);
        Log.d(TAG, msg);
        requestBuilder.post(body);
        //requestBuilder.addHeader("content-type", "application/json; charset=utf-8");
        requestBuilder.addHeader("content-type", "application/x-www-form-urlencoded");
    }
    Request request = requestBuilder.build();
    //get response with the use of the method newCall
    //synchronous request
    Response response = client.newCall(request).execute();
    //check if the request is successful
    if (response.isSuccessful()) {
        this.statusMessage = response.message();
        return response.body().string();
    } else {
        Log.d(TAG, response.toString());
        throw new IOException("Exception: response code " + response.code());
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.JSONObject) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 92 with RequestBody

use of okhttp3.RequestBody in project openstack4j by ContainX.

the class VolumeBackupTests method createVolumeBackupFromSnapshotV1.

@Test
public void createVolumeBackupFromSnapshotV1() throws Exception {
    respondWith("/storage/v1/volumebackup_create_response.json");
    final String name = "backup1122";
    VolumeBackupCreate create = Builders.volumeBackupCreate().volumeId("999b49ff-a813-45cc-aef3-3ec82f089490").container("container123").description("description123").name(name).incremental(false).snapshotId("b4b3258d-555a-4fce-8f53-69cc2ae96d3c").build();
    VolumeBackup backup = osv3().blockStorage().backups().create(create);
    RecordedRequest request = server.takeRequest();
    assertNotNull(request.getHeader("X-Auth-Token"));
    assertTrue(request.getPath().matches("/v[123]/\\p{XDigit}*/backups"));
    assertEquals(request.getMethod(), "POST");
    String requestBody = request.getBody().readUtf8();
    assertTrue(requestBody.contains("\"volume_id\" : \"999b49ff-a813-45cc-aef3-3ec82f089490\""));
    assertTrue(requestBody.contains("\"snapshot_id\" : \"b4b3258d-555a-4fce-8f53-69cc2ae96d3c\""));
    assertEquals(backup.getName(), name);
    assertNotNull(backup.getId());
    assertEquals(backup.getId(), "7069c687-c85c-45ca-befa-aa78a971fdfe");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) VolumeBackupCreate(org.openstack4j.model.storage.block.VolumeBackupCreate) VolumeBackup(org.openstack4j.model.storage.block.VolumeBackup) Test(org.testng.annotations.Test) AbstractTest(org.openstack4j.api.AbstractTest)

Example 93 with RequestBody

use of okhttp3.RequestBody in project openstack4j by ContainX.

the class VolumeBackupTests method createVolumeBackupV1.

@Test
public void createVolumeBackupV1() throws Exception {
    respondWith("/storage/v1/volumebackup_create_response.json");
    final String name = "backup1122";
    VolumeBackupCreate create = Builders.volumeBackupCreate().volumeId("999b49ff-a813-45cc-aef3-3ec82f089490").container("container123").description("description123").name(name).incremental(false).build();
    VolumeBackup backup = osv3().blockStorage().backups().create(create);
    RecordedRequest request = server.takeRequest();
    assertNotNull(request.getHeader("X-Auth-Token"));
    assertTrue(request.getPath().matches("/v[123]/\\p{XDigit}*/backups"));
    assertEquals(request.getMethod(), "POST");
    String requestBody = request.getBody().readUtf8();
    assertTrue(requestBody.contains("\"volume_id\" : \"999b49ff-a813-45cc-aef3-3ec82f089490\""));
    assertEquals(backup.getName(), name);
    assertNotNull(backup.getId());
    assertEquals(backup.getId(), "7069c687-c85c-45ca-befa-aa78a971fdfe");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) VolumeBackupCreate(org.openstack4j.model.storage.block.VolumeBackupCreate) VolumeBackup(org.openstack4j.model.storage.block.VolumeBackup) Test(org.testng.annotations.Test) AbstractTest(org.openstack4j.api.AbstractTest)

Example 94 with RequestBody

use of okhttp3.RequestBody in project Varis-Android by dkhmelenko.

the class BuildsDetailsPresenter method restartBuild.

/**
     * Restarts build process
     */
public void restartBuild() {
    RequestBody emptyBody = RequestBody.create(MediaType.parse("application/json"), "");
    Disposable subscription = mTravisRestClient.getApiService().restartBuild(mBuildId, emptyBody).onErrorReturn(throwable -> new Object()).flatMap(new Function<Object, SingleSource<BuildDetails>>() {

        @Override
        public SingleSource<BuildDetails> apply(@NonNull Object o) throws Exception {
            return mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((buildDetails, throwable) -> {
        if (throwable == null) {
            handleBuildDetails(buildDetails);
        } else {
            handleLoadingFailed(throwable);
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) BuildDetails(com.khmelenko.lab.varis.network.response.BuildDetails) NonNull(io.reactivex.annotations.NonNull) RequestBody(okhttp3.RequestBody)

Example 95 with RequestBody

use of okhttp3.RequestBody in project xmall by Exrick.

the class QiniuUtil method qiniuBase64Upload.

public static String qiniuBase64Upload(String data64) {
    String key = renamePic(".png");
    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);
    // 服务端http://up-z2.qiniup.com
    String url = "http://up-z2.qiniup.com/putb64/-1/key/" + UrlSafeBase64.encodeToString(key);
    RequestBody rb = RequestBody.create(null, data64);
    Request request = new Request.Builder().url(url).addHeader("Content-Type", "application/octet-stream").addHeader("Authorization", "UpToken " + getUpToken()).post(rb).build();
    System.out.println(request.headers());
    OkHttpClient client = new OkHttpClient();
    okhttp3.Response response = null;
    try {
        response = client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(response);
    return origin + key;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Auth(com.qiniu.util.Auth) Request(okhttp3.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)178 Request (okhttp3.Request)141 IOException (java.io.IOException)75 Response (okhttp3.Response)74 Test (org.junit.Test)53 ResponseBody (okhttp3.ResponseBody)32 Call (okhttp3.Call)31 MultipartBody (okhttp3.MultipartBody)28 MediaType (okhttp3.MediaType)27 FormBody (okhttp3.FormBody)25 Callback (okhttp3.Callback)23 Buffer (okio.Buffer)23 MockResponse (okhttp3.mockwebserver.MockResponse)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 TestClients.clientRequest (keywhiz.TestClients.clientRequest)15 BufferedSink (okio.BufferedSink)15 Headers (okhttp3.Headers)12 HttpUrl (okhttp3.HttpUrl)11 JSONObject (org.json.JSONObject)11 Body (retrofit2.http.Body)11