Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3637

How to send multi-part file with request body

$
0
0

I want to construct an API of user signup in which I need user basic information and their profile picture. I don't know how I can achieve this. I have made a controller and requested for a body, but when I access this API, it gives an error of "Unsupported Media Type", and when I set content Type as multipart/form-data, it gives an error:

the request was rejected because no multipart boundary was found

How can I send user information and user photo both in the same request?

Controller

@RequestMapping(method = RequestMethod.POST, value = "createRider")public @ResponseBody ResponseEntity<?> createRider(        @RequestBody CreateRider createRider,Authentication authentication,        PersistentEntityResourceAssembler assembler,@RequestPart(value = "profilePic", required = false) MultipartFile file) {    if (authentication != null && authentication.getPrincipal() != null) {        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();        boolean authorized = authorities.contains(new SimpleGrantedAuthority("rights"));        if (authorized==true)            userService.createNewRider(createRider);        else            return  ResponseEntity.status(HttpStatus.SC_CONFLICT).body("Logged In user is not admin");    } else {        // Access denied        throw new AccessDeniedException("Not logged in");    }    return ResponseEntity.ok("Rider Created");}

createRider.java

public class CreateRider {    private String email;    private String name;    private String password;    private String contactNumber;    private String cnicNumber;    private String drivingLicense;    private String reference;    private MultipartFile file;     ..getters nad setters     }

userService.createNewRider:

public void createNewRider(CreateRider createRider) {    Group group=groupRepo.findOne(Constants.RIDER_USER_GROUP);    User user=new User();    user.setGroup(group);    user.setEmail(createRider.getEmail());    user.setName(createRider.getName());    user.setPassword(createRider.getPassword());    user.setContactNumber(createRider.getContactNumber());    user.setCnicNumber(createRider.getCnicNumber());    user.setDrivingLicense(createRider.getDrivingLicense());    user.setReference(createRider.getReference());    userRepo.save(user);    RiderLocation riderLocation=new RiderLocation();    riderLocation.setRider(user);    riderLocationRepo.save(riderLocation);    ///User Photo      UserPhoto userPhoto=photoService.createUserPhoto(createRider.getFile(), user.getId());     userPhotoRepo.save(userPhoto);}

Viewing all articles
Browse latest Browse all 3637

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>