I am using retool pltaform and Following Google's Upload file data documentation for multipart uploads I have constructed this request body. I'm using the result of Filedrop's base64 as the binary file in my request body.
the file is upload to the correct folder and get the right name and the right type but
I keep getting the "No preview available" when i open the image or PDF , i don't know which part of the request body is wrong...
Any support would be appreciated.
url and headers
--foo_bar_baz
Content-Type: application/json; charset=UTF-8
{'name': "{{filesDrop.files[0].name}}",'mimeType':'{{filesDrop.files[0].type}}','parents':['DRIVEID']}
--foo_bar_baz
Content-Type: {{filesDrop.files[0].type}}
Content-Transfer-Encoding: BASE64
{{filesDrop.value[0]}}//in base64 format
--foo_bar_baz--
let body = ""
let boundry = "--YOURBOUNDRY"
let rn = "\r\n"
// set file metadata
body += boundry + rn;
body += "Content-Type: application/json" + rn + rn;
body += "{\"name\":\"" + filesDrop.files[0].name + "\"}" + rn
// add actual file contents
body += boundry + rn;
body += "Content-Type: " + filesDrop.files[0].type + rn;
body += "Content-Transfer-Encoding: base64" + rn + rn;
body += filesDrop.value[0] + rn;
body += boundry + "--";
return body;
Related
I created an endpoint that takes a json body from a POST request, uses that data to fill in the fields of a PDF, and send the filled out file back. When I try to send a POST request using Send and Download on Postman, I initially get a 200 OK back and Postman goes into a loading state, showing how much time the download has taken so far.
After about 2 minutes of this, I get an ECONNRESET error:
Thinking this was just a problem with Postman, I updated a React project of mine to hit the endpoint. I was expecting making the request would start the browser's built in file download feature. Instead, I got a similar error: ERR_CONNECTION_RESET.
I debugged my controller and it seems to be parsing the request body correctly. It also doesn't seem to take too long to return from the endpoint's function. I'm using the File method from ControllerBase to make the response from the file stream, and I make sure not to dispose of the file stream too early.
Per Ali Vahidinasab's request, here is the Postman request exported to C#:
var client = new RestClient("https://localhost:44398/api/charactersheet/download");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/pdf");
var body = #"{
" + "\n" +
#" ""abilityScores"": {
" + "\n" +
#" ""strength"": 10,
" + "\n" +
#" ""dexterity"": 11,
" + "\n" +
#" ""constitution"": 12,
" + "\n" +
#" ""intelligence"": 13,
" + "\n" +
#" ""wisdom"": 14,
" + "\n" +
#" ""charisma"": 15
" + "\n" +
#" }
" + "\n" +
#"}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine($"Status Code: {response.StatusCode}");
When I ran this client code, the response had a status code of 0, which I understand means that there is no response from the server.
I guess the crux of this question is: is this an issue with the server or the client? And what can I do to fix it?
I found the issue. It was on the server side. I had to set the position of the memory stream I was returning to 0 before returning it.
How to call a REST API from javascript with ajax or XMLHttpRequest to upload a file using
Content-Type: multipart/form-data.
File content is in binary format, but the API which I am calling has following request format:
Authorization: Bearer <>
Content-Type: multipart/form-data
I am using following code segment to upload the file content:
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", requestUrl);
xmlHttp.setRequestHeader("Authorization", "Bearer " + token);
xmlHttp.setRequestHeader("Content-Type", "multipart/form-data");
xmlHttp.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xmlHttp.send(formData);
Where formData is the filecontent in binary format. Please suggest if this is the right way or should be handled differently.The file type I am using is an IFC file. And the error I am receiving is media-type not supported
Thanks!
For uploading files to Autodesk Forge (A360), the following curl command works in a Unix-like terminal:
curl -v 'https://developer.api.autodesk.com/oss/v2/buckets/$bucket_name/objects/$filename.ifc' \
-X 'PUT' \
-H 'Authorization: Bearer $token' \
-H 'Content-Type: text/plain; charset=UTF-8' \
-T '$filename.ifc'
since IFC files are text (ASCII) files and not binary files.
Using the same Content-Type might do the trick in your case.
Hope it helps.
I'm seeing some odd behavior with a cookie on the server side, and would like to understand why.
On the client:
document.cookie = 'test_cookie=' + '[AB]cd|ef-gh[IJ]' + '; path=/;';
document.cookie = 'test_cookie2=' + 'cd|ef-gh' + '; path=/;';
On the server:
headers = httpServletRequest.getHeaders()
// iterate and print headers
cookies = httpServletRequest.getCookies();
// iterate and print headers
Output:
// Both are there on the header, so tomcat doesn't block it:
...
header: cookie: test_cookie=[AB]cd|ef-gh[IJ]; test_cookie2=cd|ef-gh
// Only one shows up from getCookies()
...
cookie: test_cookie2=cd|ef-gh
// no test_cookie ???
Why am I not able to see the test_cookie2?
I could uri-encode before I set it on the client, but I thought '[' and ']' were allowed cookie characters?
Is there a more correct way to set it?
Here's the way to set the cookie correctly on the frontend:
document.cookie = 'test_cookie="[AB]cd|ef-gh[IJ]"; path=/';
Not the double quotes around the cookie value that contains the special characters.
I'm now already trying for hours, but somehow i don't figure out the problem. Here is a sample of my Request:
POST /test/upload/upload.php HTTP/1.0
Host: localhost
User-Agent: TestBrowser
Content-Type: multipart/form-data, boundary=635131229269 //edited
Content-Length: 94
--635131229269
Content-Disposition: form-data; name="testme"
contender
--635131229269--
It is sended over standard TCP/IP Socket to a PHP Server, but the $_POST['testme'] Value is always empty.
Does someone can see the bug in this Request? --> solved
There is a \r\n at the end which doesn't show in the Code here.
Thank you, that solved my first problem.
Maybe you can help my with my second aswell. As i had seen on your profile you are well with C# and there is my second problem. I'm trying to upload a file to my server and the filedata somehow does not requested properly, i think it's because of the encoding, but i'm not sure.
_content = _content
+ "--" + boundary + Environment.NewLine
+ "Content-Disposition: form-data; name=\"" + this._FileVarName + "\"; filename=\"" + Path.GetFileName(this._FilePath) + "\""
+ Environment.NewLine + "Content-Transfer-Encoding: application/octet-stream"
+ Environment.NewLine + Environment.NewLine;
mainContent = this.Combine(Encoding.UTF8.GetBytes(_content), StreamFile(this._FilePath));
private Byte[] StreamFile(string Path)
{
FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read);
Byte[] ImageData = new byte[fs.Length];
fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
return ImageData;
}
You have to specify the boundary (on line 4) without the leading dashes. Also I only count 92 bytes of payload.
Just to make this answer a bit more complete, there has to be a semicolon after "multipart/form-data" (see w3c forms spec)
Content-Type: multipart/form-data; boundary=635131229269
I have an APEX Method that attempts to Post a form to a remote endpoint out of SFDC's APEX Code.
Everything seems to encode correctly, and the server sends back a 200 response, but the attachment isn't arriving with the request... is SFDC removing the content of my post body before it's sent?
HttpRequest req = new HttpRequest();
req.setHeader('Authorization','Basic '+EncodingUtil.base64Encode(Blob.valueOf('removed:removed')));
req.setHeader('Content-Type','multipart/form-data; boundary=-----------------------------153501500631101');
req.setHeader('X-Atlassian-Token','nocheck');
req.setMethod('POST');
req.setEndpoint(endPoint+'issue/'+c.Internal_Bug_Number__c+'/attachments');
String body = '-----------------------------153501500631101\r\n';
body = body + 'Content-Disposition: form-data; name="Filedata"; filename="'+attachments[0].Name+'"\r\n';
body = body + 'Content-Type: '+attachments[0].ContentType+'\r\n';
body = body + 'Content-transfer-encoding: base64\r\n\r\n';
body = body + attachments[0].Body+ '\r\n';
body = body + '-----------------------------153501500631101--\r\n';
req.setBody(body);
Try this solution. It's a messing solution with Blob+HttpResponse I've proposed. http://enreeco.blogspot.it/2013/01/salesforce-apex-post-mutipartform-data.html
You should be able to use EncodingUtil.urlEncode to ensure the form params. See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_encodingUtil.htm