Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filename to Content-Disposition header in Wiki (Recipes) #1521

Closed
prrrnd opened this issue Mar 20, 2015 · 2 comments
Closed

Add filename to Content-Disposition header in Wiki (Recipes) #1521

prrrnd opened this issue Mar 20, 2015 · 2 comments
Labels
documentation Documentation task
Milestone

Comments

@prrrnd
Copy link

prrrnd commented Mar 20, 2015

Under Posting a multipart request

  private static final String IMGUR_CLIENT_ID = "...";
  private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

  private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
    RequestBody requestBody = new MultipartBuilder()
        .type(MultipartBuilder.FORM)
        .addPart(
            Headers.of("Content-Disposition", "form-data; name=\"title\""),
            RequestBody.create(null, "Square Logo"))
        .addPart(
            Headers.of("Content-Disposition", "form-data; name=\"image\"; filename=\"logo-square.png\""),
            RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
        .build();

    Request request = new Request.Builder()
        .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
        .url("https://api.imgur.com/3/image")
        .post(requestBody)
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    System.out.println(response.body().string());
  }
@swankjesse
Copy link
Member

See addFormDataPart() overload which includes a filename.

@swankjesse swankjesse added the documentation Documentation task label Mar 25, 2015
@swankjesse swankjesse added this to the 2.4 milestone Apr 4, 2015
@swankjesse swankjesse modified the milestones: 2.5, 2.4 May 23, 2015
@JakeWharton JakeWharton modified the milestones: 2.5, 2.6 Sep 1, 2015
swankjesse added a commit that referenced this issue Nov 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation task
Projects
None yet
Development

No branches or pull requests

3 participants