Skip to content

Instantly share code, notes, and snippets.

@mizanRahman
Created May 31, 2016 19:39
Show Gist options
  • Select an option

  • Save mizanRahman/dd59d81c3975cffc0ca8dded4a2f6187 to your computer and use it in GitHub Desktop.

Select an option

Save mizanRahman/dd59d81c3975cffc0ca8dded4a2f6187 to your computer and use it in GitHub Desktop.
File Upload Sample
package com.hello.app;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.ResponseBody;
import retrofit.Call;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.Part;
import java.io.File;
import java.io.IOException;
/**
* Created by mac on 6/1/16.
*/
public class App {
public static final String API_URL = "http://localhost:8000";
public static void main(String[] args) throws IOException {
System.out.println("started");
testApiRequest();
}
/**
* HttpBin.org service definition
*/
public interface HttpBinService {
@Multipart
@POST("upload")
Call<ResponseBody> upload(@Part("description") RequestBody requestBody);
}
public static void testApiRequest() throws IOException {
// Retrofit setup
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
// Service setup
HttpBinService service = retrofit.create(HttpBinService.class);
RequestBody requestBody = new MultipartBuilder()
.addFormDataPart("name", "mizanur rahman khan")
.addFormDataPart("image", "logo-square.png",
RequestBody.create(MediaType.parse("text/xml"),
new File("/Users/mac/Desktop/standalone.xml")))
.build();
// Prepare the HTTP request
Call<ResponseBody> call = service.upload(requestBody);
Response<ResponseBody> response = call.execute();
System.out.println("=====>> Successful: " + response.isSuccess()+ " <<=============");
}
}
'use strict';
const Hapi = require('hapi');
var util = require('util');
// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
// Add the route
server.route({
method: 'GET',
path:'/hello',
handler: function (request, reply) {
return reply('hello world');
}
});
server.route([
{
method: 'POST',
path: '/upload',
config: {
payload: {
maxBytes: 209715200,
output: 'stream',
parse: false
},
handler: function(request, reply) {
var multiparty = require('multiparty');
var form = new multiparty.Form();
form.parse(request.payload, function(err, fields, files) {
console.log(fields);
console.log(files);
console.log("ok");
});
console.log("i am done");
return reply("ok");
}
}
}
]);
// Start the server
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment