Home | History | Annotate | Download | only in recipes
      1 /*
      2  * Copyright (C) 2014 Square, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.squareup.okhttp.recipes;
     17 
     18 import com.squareup.okhttp.MediaType;
     19 import com.squareup.okhttp.OkHttpClient;
     20 import com.squareup.okhttp.Request;
     21 import com.squareup.okhttp.RequestBody;
     22 import com.squareup.okhttp.Response;
     23 import java.io.File;
     24 import java.io.IOException;
     25 
     26 public final class PostFile {
     27   public static final MediaType MEDIA_TYPE_MARKDOWN
     28       = MediaType.parse("text/x-markdown; charset=utf-8");
     29 
     30   private final OkHttpClient client = new OkHttpClient();
     31 
     32   public void run() throws Exception {
     33     File file = new File("README.md");
     34 
     35     Request request = new Request.Builder()
     36         .url("https://api.github.com/markdown/raw")
     37         .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
     38         .build();
     39 
     40     Response response = client.newCall(request).execute();
     41     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
     42 
     43     System.out.println(response.body().string());
     44   }
     45 
     46   public static void main(String... args) throws Exception {
     47     new PostFile().run();
     48   }
     49 }
     50