How to create a FastAPI endpoint that can accept either Form or JSON body?

Option 1 You could do that by having a dependency function, where you check the value of the Content-Type request header and parse the body using Starlette’s methods, accordingly. Note that just because a request’s Content-Type header says, for instance, application/json, application/x-www-form-urlencoded or multipart/form-data, doesn’t always mean that this is true, or that the incoming … Read more

Jersey 2 injection source for multipart formdata

You need to enable MultiPart feature on your application. Enabling this feature injects necessary message body readers, writers to your Jersey 2 application. Here is how you register them: On the server-side (http-server): final ResourceConfig resourceConfig = new ResourceConfig(MultiPartResource.class); resourceConfig.register(MultiPartFeature.class); On the server-side (servlet deployment): import org.glassfish.jersey.filter.LoggingFilter; import org.glassfish.jersey.media.multipart.MultiPartFeature; import javax.ws.rs.core.Application; import java.util.HashSet; import java.util.Set; … Read more

How to upload multipart form data and image to server in android?

If like me you were struggling with multipart upload. Here’s a solution using 95% of code from this Android snippet. public String multipartRequest(String urlTo, Map<String, String> parmas, String filepath, String filefield, String fileMimeType) throws CustomException { HttpURLConnection connection = null; DataOutputStream outputStream = null; InputStream inputStream = null; String twoHyphens = “–“; String boundary = … Read more

Retrofit Uploading multiple images to a single key

We can use MultipartBody.Part array to upload an array of images to a single key. Here is the solution WebServicesAPI @Multipart @POST(WebServices.UPLOAD_SURVEY) Call<UploadSurveyResponseModel> uploadSurvey(@Part MultipartBody.Part[] surveyImage, @Part MultipartBody.Part propertyImage, @Part(“DRA”) RequestBody dra); Here is the method for uploading the files. private void requestUploadSurvey () { File propertyImageFile = new File(surveyModel.getPropertyImagePath()); RequestBody propertyImage = RequestBody.create(MediaType.parse(“image/*”), propertyImageFile); … Read more

Can I append an array to ‘formdata’ in javascript?

How about this? formdata.append(‘tags’, JSON.stringify(tags)); … and, correspondingly, using json_decode on server to deparse it. See, the second value of FormData.append can be… a Blob, File, or a string, if neither, the value is converted to a string The way I see it, your tags array contains objects (@Musa is right, btw; making this_tag an … Read more

NodeJS, Axios – post file from local server to another server

The 2 oldest answers did not work for me. This, however, did the trick: const FormData = require(‘form-data’); // npm install –save form-data const form = new FormData(); form.append(‘file’, fs.createReadStream(file.path)); const request_config = { headers: { ‘Authorization’: `Bearer ${access_token}`, …form.getHeaders() } }; return axios.post(url, form, request_config); form.getHeaders() returns an Object with the content-type as well … Read more

simple HttpURLConnection POST file multipart/form-data from android to google blobstore

use okhttp and use following snippet (taken from recipes) adjust the header values according to what your server expects. private static final String IMGUR_CLIENT_ID = “…”; private static final MediaType MEDIA_TYPE_PNG = MediaType.parse(“image/png”); private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { // Use the imgur image upload API as documented … Read more

Node.js (with express & bodyParser): unable to obtain form-data from post request

In general, an express app needs to specify the appropriate body-parser middleware in order for req.body to contain the body. [EDITED] If you required parsing of url-encoded (non-multipart) form data, as well as JSON, try adding: // Put this statement near the top of your module var bodyParser = require(‘body-parser’); // Put these statements before … Read more

tech