Components for http post request

Highlighted
Four Stars

Components for http post request

Hello,

I want to make a http post request to sending a file in parameter and return a file. The java code below works. With which component and what parameters can we do the equivalent? (I tested tfileFetch, tHttpPost etc ... but maybe with parameter errors)

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


public class Adresses {

public static void GeocodeAdresses(String cheminFichierSource, String cheminFichierCible) {

// Préparation du fichier à uploader
File fichierSource = new File(cheminFichierSource.replace("/", "\\"));
FileInputStream fichierUpload = null;
try {
fichierUpload = new FileInputStream(fichierSource);
CloseableHttpClient httpclient = HttpClients.createDefault();

// URL API
HttpPost httppost = new HttpPost("https://api-adresse.data.gouv.fr/search/csv/");
MultipartEntityBuilder entity = MultipartEntityBuilder.create();

// Paramètres
entity.addPart("data", new InputStreamBody(fichierUpload, fichierSource.getName()));
httppost.setEntity(entity.build());

// Exécution de la requête
HttpResponse reponse = httpclient.execute(httppost);

// Code retour
int statusCode = reponse.getStatusLine().getStatusCode();
System.out.println("Code retour : " + statusCode);

// Téléchargement du fichier
HttpEntity reponseEntity = reponse.getEntity();
BufferedInputStream bis = new BufferedInputStream(reponseEntity.getContent());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(cheminFichierCible.replace("/","\\"))));
int inByte;
while((inByte = bis.read()) != -1) bos.write(inByte);
bis.close();
bos.close();

} catch (ClientProtocolException e) {
System.err.println("Connexion impossible");
e.printStackTrace();
} catch (IOException e) {
System.err.println("Lecture du fichier impossible");
e.printStackTrace();
} finally {
try {
if (fichierUpload != null) fichierUpload.close();
} catch (IOException e) {}
}
}

},

Thanks for your help,
Pauline

Eight Stars sgv
Eight Stars

Re: Components for http post request

Hi ,

 

I don't think that you can pass a file in a post method with a talend component. Maybe in a premium version ...

But check your private message. We can talk about this.

Good luck,

SGV

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog