Home > Back-end >  Problem sending image to endpoint, error "status":500,"error":"Internal Ser
Problem sending image to endpoint, error "status":500,"error":"Internal Ser

Time:11-21

I need to send a picture to a third-party api, but an error occurs {"timestamp": "2021-11-15T15: 25: 06.532 00: 00", "status": 500, "error": "Internal Server Error", "path ":" / api / send / "}

If I contact the service directly (postman http://exampleResourese/sendimg), then I get the answer normally, then the problem is in my code (on endpoint http://localhost:8091/api/send).

Tell me how to send a picture via RestTemlate or another method.

a third-party service accepts a picture string ($binary)

Controller:

   @RequestMapping(value = "/send",method = RequestMethod.POST)
     public ResponseEntity<?> sendCopyPassport(@RequestParam("images") MultipartFile files) throws MalformedURLException, IOException{
    
        return documentsReceivingService.uploadAndGetListDocuments(files);
    
}

Service :

@Service public class DocumentsReceivingService {

   @Autowired
    RestTemplate restTemplate;

   private final String UPLOADFILE = "http://exampleResourese/sendimg";


 Logger logger = LoggerFactory.getLogger(DocumentsReceivingService.class);

    public ResponseEntity<?> uploadAndGetListDocuments(MultipartFile files) throws MalformedURLException, IOException{


LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
     Charset charset = Charset.forName("UTF-8");
        String str =null;
       
       
       byte[] bytes =null;
try{

bytes = files.getBytes();
 str = new String(bytes,charset);
} catch (IOException e) {
            
            ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body( new MessageResponse("err"));
        }

   map.add("images",str);
   HttpHeaders headers = new HttpHeaders();
   headers.setContentType(MediaType.MULTIPART_FORM_DATA);

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
String response = restTemplate.postForEntity(UPLOADFILE, requestEntity, String.class);

return  ResponseEntity.ok(response);

}

Error:

Response 500 INTERNAL_SERVER_ERROR
2021-11-15 20:43:00.938 DEBUG 22308 --- [nio-8091-exec-2] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: "{"error": "Serious unexpected error"}"
2021-11-15 20:43:00.938 ERROR 22308 --- [nio-8091-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: "{"error": "Serious unexpected error"}"] with root cause

org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: "{"error": "Serious unexpected error"}"

Please tell me how to correctly transfer the picture? This picture will be loaded from the front and then to my server, after which I will transfer it further to the endpoint

CodePudding user response:

If you use the service as a proxy (and you are not going to download the file to your director), then try to transfer the file by requesting json to base64 (encode bytes into base64). For example :

{ 
  "file": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA .. "
}

Check in what form the third-party service will accept.

If, nevertheless, you want to use multipart, then save it to yourself somewhere in the directory and from there upload the file to another service (since you say that if you use postman, it sends it normally, and it sends it from your PC directory), in this case the File class will help. Test, if there are problems, then show the result of the execution.

  • Related