Forum Discussion

dgdragon's avatar
dgdragon
Frequent Visitor
8 years ago
Solved

Import PBIX via Postman

I am trying to import a PBIX file using postman, but all I get is a 400 with empty message. I have tried many ways, form-data, json, binary, so I must be missing something. Anyone able to do this in ...
  • dgdragon's avatar
    dgdragon
    8 years ago

    I was able to get Fiddler to work and used that to fix my Java version. I've included the script below (thanks to Zhang)

     

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Date;
    
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.impl.client.HttpClientBuilder;
    
    
    public class ImportPBIXUtil {
     public static void main( String[] args ) {
       try {
         Import();
       } catch ( UnsupportedOperationException e ) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       } catch ( IOException e ) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
    public static void Import() throws UnsupportedOperationException, IOException {
            String bearer = "Bearer {token}";        
            String fileName = "myReport.pbix";
            String filePath = "C:\\Users\\JohnDoe\\Documents\\PowerBI\\" + fileName;
            String groupId = "{group_id}";
    
            HttpClient request = HttpClientBuilder.create().build();
    
            HttpPost post = new HttpPost( "https://api.powerbi.com/v1.0/myorg/groups/" + groupId + "/imports?datasetDisplayName=MyNewReport" );
    
            long time = (new Date()).getTime();
            String boundary = "---------------------------" + time;
            post.setHeader( "ContentType", "multipart/form-data; boundary=" + boundary );
            post.setHeader( "Authorization", bearer );
    
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setBoundary( boundary );
            builder.addBinaryBody( "filename", new File( filePath ), ContentType.APPLICATION_OCTET_STREAM, fileName );
            HttpEntity multipart = builder.build();
    
            post.setEntity( multipart );
            HttpResponse response = null;
    
            System.out.println( "Request: " );
            for (Header header : post.getAllHeaders()) {
                System.out.println( header.getName() + " : " + header.getValue() );
            }
            
            System.out.println( post.getEntity().getContentLength() );
            
            /*/
            BufferedReader reqd = new BufferedReader( new InputStreamReader( post.getEntity().getContent() ) );
    
            StringBuffer bbody = new StringBuffer();
            String l = "";
            while ( (l = reqd.readLine()) != null ) {
                bbody.append( l );
            }
            System.out.println( bbody.toString() );
            /*/
            
            System.out.println( "Response: " );
            try {
                response = request.execute( post );
            } catch ( ClientProtocolException e ) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch ( IOException e ) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            BufferedReader rd = new BufferedReader( new InputStreamReader( response.getEntity().getContent() ) );
    
            StringBuffer result = new StringBuffer();
            String line = "";
            while ( (line = rd.readLine()) != null ) {
                result.append( line );
            }
            System.out.println( response.getStatusLine().getStatusCode() );
            System.out.println( result.toString() );
    
            return;
      }
    }