Home > Software engineering >  How to use postman for testing get request given below?
How to use postman for testing get request given below?

Time:10-14

I want to create an android app to perform file operations in google drive which uses Spring boot as backend. So I had searched so many times in google and finally I got this one. But they didn't mentioned about the working. If anyone knows please help me. And please suggest some good tutorial to perform file operations in google drive using Spring boot rest api.

Get Request to create a directory in Google Drive

@GetMapping("/directory/create")
  public ResponseEntity<String> createDirectory(@RequestParam String path) throws Exception {
    String parentId = fileManager.getFolderId(path);
    return ResponseEntity.ok("parentId: " parentId);
}

getFolderId Function

public String getFolderId(String path) throws Exception {
    String parentId = null;
    String[] folderNames = path.split("/");

    Drive driveInstance = googleDriveManager.getInstance();
    for (String name : folderNames) {
        parentId = findOrCreateFolder(parentId, name, driveInstance);
    }
    return parentId;
}

findOrCreateFolder Function to create if given folder does not exist in google drive

private String findOrCreateFolder(String parentId, String folderName, Drive driveInstance) throws Exception {
    String folderId = searchFolderId(parentId, folderName, driveInstance);
    // Folder already exists, so return id
    if (folderId != null) {
        return folderId;
    }
    //Folder dont exists, create it and return folderId
    File fileMetadata = new File();
    fileMetadata.setMimeType("application/vnd.google-apps.folder");
    fileMetadata.setName(folderName);

    if (parentId != null) {
        fileMetadata.setParents(Collections.singletonList(parentId));
    }
    return driveInstance.files().create(fileMetadata)
            .setFields("id")
            .execute()
            .getId();
}

Post request to upload file to google drive

@PostMapping(value = "/upload",
        consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
        produces = {MediaType.APPLICATION_JSON_VALUE} )
public ResponseEntity<String> uploadSingleFileExample4(@RequestBody MultipartFile file,@RequestParam(required = false) String path) {
    logger.info("Request contains, File: "   file.getOriginalFilename());
    String fileId = fileManager.uploadFile(file, path);
    if(fileId == null){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    return ResponseEntity.ok("Success, FileId: "  fileId);
}

Upload function to upload file to google drive

public String uploadFile(MultipartFile file, String filePath) {
    try {
        String folderId = getFolderId(filePath);
        if (file != null) {
            File fileMetadata = new File();
            fileMetadata.setParents(Collections.singletonList(folderId));
            fileMetadata.setName(file.getOriginalFilename());
            File uploadFile = googleDriveManager.getInstance()
                    .files()
                    .create(fileMetadata, new InputStreamContent(
                            file.getContentType(),
                            new ByteArrayInputStream(file.getBytes()))
                    )
                    .setFields("id").execute();
            return uploadFile.getId();
        }
    } catch (Exception e) {
        System.out.print("Error: " e);
    }
    return null;
}

Reference https://technicalsand.com/file-operations-in-google-drive-api-with-spring-boot/

CodePudding user response:

To test the above get request in postman. Assume you are running on localhost:8080 then request will be -

localhost:8080?path="directory_name"

CodePudding user response:

Testing GET request in Postman: Image

If you are running in local,

http://{host}:{port}/{endpoint}

http://localhost:8080/directory/create

  • Related