php Editor Xigua will introduce you how to use image files to send JSON in Postman. Postman is a powerful API development tool that can help developers perform interface testing and debugging. Normally, we use Postman to send JSON data, but what if we need to include image files in JSON? This article will introduce in detail how to implement this function in Postman through some simple steps, allowing you to use Postman for interface testing more flexibly. Whether you're a beginner or an experienced developer, you'll gain practical tips and knowledge from this article. Let’s find out together!
I want to send a json file with images in postman but I am getting the following error:
"status": 415, "error": "unsupported media type",
package com.shayanr.homeservicespring.controller; import com.shayanr.homeservicespring.entity.users.expert; import com.shayanr.homeservicespring.service.expertservice; import lombok.requiredargsconstructor; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.multipartfile; import java.io.file; import java.io.ioexception; @restcontroller @requestmapping("/expert") @requiredargsconstructor public class expertcontroller { private expertservice expertservice; @postmapping("/register") public void register(@requestbody expert expert, @requestparam("image") multipartfile image) throws ioexception { expertservice.signup(expert, image); } }
@override @transactional public expert signup(expert expert, multipartfile image) throws ioexception { if (expert == null) { throw new nullpointerexception("null expert"); } if (!validate.namevalidation(expert.getfirstname()) || !validate.namevalidation(expert.getlastname()) || !validate.emailvalidation(expert.getemail()) || !validate.passwordvalidation(expert.getpassword())) { throw new persistenceexception("wrong validation"); } string extension = filenameutils.getextension(image.getname()); if (extension.equalsignorecase("video") || extension.equalsignorecase("mp4")) { throw new persistenceexception("invalid image format. only image files are allowed."); } expert.setconfirmation(confirmation.new); byte[] imageinbytes = image.getbytes(); expert.setimage(imageinbytes); expertrepository.save(expert); return expert; }
In postman my first line is json and this json file
{ "firstName": "Sasa", "lastName": "Weel", "email": "[email protected]", "password": "Sasasd1@", "signUpDate": "2024-01-31", "signUpTime": "10:00:00" }
In the second line I set the image where the problem is
Your @requestparam("image") multipartfile image
parameter looks correct .
So the next thing you should do is make sure you tell spring that you want to allow multipart files.
This is an example application.properties
Configuration:
spring.servlet.multipart.enabled=true
Additional configuration settings that control upload size:
# Adjust these file size restrictions and location as nessessary spring.servlet.multipart.max-file-size=2MB spring.servlet.multipart.max-request-size=10MB spring.servlet.multipart.location=/temp
I suspect that the @requestbody expert expert
parameter should be related to this.
The above is the detailed content of Send JSON using image files in Postman. For more information, please follow other related articles on the PHP Chinese website!