What does the random string after a WebKitFormBoundary do \ mean?
That's just the typical way of how a so called "boundary" between different parts of a mime structure is defined. The receiving side can tell the different parts apart by this. Same logic is used in different things, email messages too for example.
That "random" string is indeed random, all boundary markers using an identical string are "grouped", so working on the same level. Mime part structures can stacked in a hierarchical manner. In such case different random strings are used in different levels to tell them apart. This is for example how citing an email as attachment to a new email. If the cited email contains multiple mime parts, their boundaries must be different to those of the new email, otherwise confusion would arise between the levels.
Actually it is not the "random part" of that boundary that counts. The whole string is matched. It is simply a convention that each software uses a unique prefix string for such boundaries for transparency reasons. But in general the only requirement is that the chosen string must be unique throughout all contained data. Unique obviously except for the corresponding boundaries which must use exactly the same string.
Recreate POST request with WebKitFormBoundary using Python's requests
The exact name of the boundary does not matter as long as it is declared in the header:
Content-Type: multipart/mixed; boundary=gc0p4Jq0M2Yt08jU534c0p
With this header the boundaries would be
--gc0p4Jq0M2Yt08jU534c0p
There server will take a look at the Content-Type
header and figure out the body parts.
Send file to the server with Retrofit and WebKitFormBoundary in PartMap
So I resolve this problem in this way
Request to my server. request without header "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"" which described in API documentation.
@Multipart
@Headers({"Accept: application/json"})
@POST("api/save")
Call<SaveResponse> save(@Header("Authorization") String authorization,
@PartMap Map<String, RequestBody> map,
@Part List<MultipartBody.Part> files);
Dynamically fill our query
private void fillQuery() {
fileListPart = new ArrayList<>();
MultipartBody.Part filePart = null;
if (mainFile != null) {
if (mainFile.getName().endsWith(".pdf")) {
filePart = MultipartBody.Part.createFormData("filename", mainFile.getName(), RequestBody.create(MediaType.parse("application/pdf"), mainFile));
} else if (mainFile.getName().endsWith(".doc") || mainFile.getName().endsWith(".docx")) {
filePart = MultipartBody.Part.createFormData("filename", mainFile.getName(), RequestBody.create(MediaType.parse("application/msword"), mainFile));
}
fileListPart.add(filePart);
}
requestBodyMap = new HashMap<>();
RequestBody id = RequestBody.create(MediaType.parse("text/plain"), "id");
RequestBody type = RequestBody.create(MediaType.parse("text/plain"), "type");
RequestBody notes = RequestBody.create(MediaType.parse("text/plain"), "notes");
requestBodyMap.put("id", id);
requestBodyMap.put("type", type);
requestBodyMap.put("notes", notes);
for (int i = 0; i < dynamicCollectionWirhParameters.size(); i++) {
String prefix = "list[";
String checkIdParameter = "][id]";
String checkIsCheckedParameter = "][is_checked]";
String checkFileParameter = "][filename]";
RequestBody checklist_id = RequestBody.create(MediaType.parse("text/plain"), "2");//id
RequestBody is_checked = RequestBody.create(MediaType.parse("text/plain"), "1"); // server convert 1 and 0 to boolean value
requestBodyMap.put(prefix + i + checkIdParameter, checklist_id);
requestBodyMap.put(prefix + i + checkIsCheckedParameter, is_checked);
if (dynamicCollectionFileCollection.get(i) != null) {
File tempFile = dynamicCollectionFileCollection.get(i);
filePart = null;
if (tempFile.getName().endsWith(".pdf")) {
filePart = MultipartBody.Part.createFormData(prefix + i + checkFileParameter, dynamicCollectionFileCollection.get(i).getName(), RequestBody.create(MediaType.parse("application/pdf"), dynamicCollectionFileCollection.get(i)));
} else if (dynamicCollectionFileCollection.get(i).getName().endsWith(".doc") || dynamicCollectionFileCollection.get(i).getName().endsWith(".docx")) {
filePart = MultipartBody.Part.createFormData(prefix + i + checkFileParameter, dynamicCollectionFileCollection.get(i).getName(), RequestBody.create(MediaType.parse("application/msword"), dynamicCollectionFileCollection.get(i)));
}
fileListPart.add(filePart);
}
}
}
And send our datas to the server
private void saveDatas() {
fillQuery();
Call<SaveResponse> sendDatasCall = DependenciesStorage.getRetrofitService().save(
DependenciesStorage.getAuthorizationKey(),
requestBodyMap, fileListPart);
sendDatasCall.enqueue(new Callback<SaveResponse>() {
@Override
public void onResponse(Call<SaveResponse> call, Response<SaveResponse> response) {
if (response.isSuccessful()) {
} else {
}
}
@Override
public void onFailure(Call<SaveResponse> call, Throwable throwable) {
}
});
}
Looks easy ) But i spend almost two days to find it
PHP get WebKitFormBoundary name and it's value
You can use the PHP variables $_GET
or $_REQUEST
for this. Then you get a key / value array. In your case:
array:4 [▼
"username" => "james
"email" => "james@example.com"
"language" => "en"
"message" => "hello world"
]
And echo $_GET['username'];
will print you: james
.
how to remove WebKitFormBoundary from uploaded file
So I solved the issue by the following steps :
use a file reader to get the contents of the file that I want to "upload".
grab that content and put it straight into request body.
the code as following
const handleFile = (e) => {
e.preventDefault();
const content = e.target.result;
setSelectedFile(content);
};
const handleChangeFile = (file) => {
const fileData = new FileReader();
fileData.onloadend = handleFile;
fileData.readAsText(file);
};
Related Topics
Call Ajax or Submit the Button When Checkbox Is Checked
Deselect All Options in Multiple Select With 1 Option
Javascript/Dom: How to Remove All Event Listeners of a Dom Object
How to Get the Timezone Name in JavaScript
Usestate Hook Not Updating a Value
Javascript Dashes in Phone Number
Javascript Check If Character Is a Vowel
Modify Classname When Element Is Clicked in React
Check If My HTML Table Is Empty Using JavaScript
How to Pass Value to a Onclick Function in (Jade)Pug
Basic Open Source JavaScript Image Editor
React - How to Open Pdf File as a Href Target Blank
React.Js - Input Losing Focus When Rerendering
Getting Selected Values from Dropdown Inside Table