i'm facing a strange problem with apache benchmark post file.
I need to stress a feature that handles file upload. So, I googled about, and found a post describing how to build a post file properly. Its contents looks like:
--1234567
Content-Disposition: form-data; name="user_id"
3
--1234567
Content-Disposition: form-data; name="file"; filename="cf_login.png"
Content-Type: image/png
[base64 encoded file content]
--1234567--
The ab line is this:
$ ab -c 1 -n 5 -v 4 -T 'multipart/form-data; boundary=1234567' -p post_file.txt http://localhost:3000/files
When ab make the requests the generated header is the following:
INFO: POST header ==
---
POST /files.json/ HTTP/1.0
Content-length: 229
Content-type: multipart/form-data; boundary=simpleboundary
Host: localhost:3000
User-Agent: ApacheBench/2.3
Accept: */*
---
LOG: header received:
HTTP/1.1 500 Internal Server Error
Content-Type: text/html; charset=utf-8
Content-Length: 13265
X-Request-Id: 9ad57d19cd71886a1bb817d00fac651b
X-Runtime: 0.015504
Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
Date: Tue, 25 Sep 2012 13:54:29 GMT
Connection: close
The expected return is a raise params.inspect, that let me see if the data is arriving to the other side. If I remove the boundary I can see data received in params, but this is not what I want. I want to get a file upload.
Anyone has a tip? I will really appreciate it.
I found the answer, and decided to share with you.
After some struggle with the post file, I decided to create a php script just to output the $_FILES and $_REQUEST variables to see if the file was built correctly. Indeed, apache receive the file perfectly and I could see the file data, and other request params.
With Rails the same doesn't occur, and after reading the documentation of HTTP 1.1 multipart topic, I realize that the problem was related to the post file format.
When you built this kind of file, you need to build it in details, and this means include all the special characters like \r and \n in the right place. The multipart does now work with "\n" at all, it needs "dos" line ending "\r\n".
So the correct post file looks like this (the \r\n is for illustration, but should be there, you know?):
--boundary_hash\r\n
Content-Disposition: form-data; name="your_form_field";\r\n
Content-Type: text/plain\r\n
\r\n
your form field data\r\n
--boundary_hash\r\n
Content-Disposition: form-data; name="another_field";\r\n
Content-Type: text/plain\r\n
\r\n
another field data\r\n
--boundary_hash\r\n
Content-Disposition: form-data; name="filename"; filename="cf_login.png"\r\n
Content-Type: image/png\r\n
\r\n
base64 file content\r\n
--boundary_hash--\r\n
When you open this file you see this actually:
--boundary_hash
Content-Disposition: form-data; name="your_form_field";
Content-Type: text/plain
your form field data
--boundary_hash
Content-Disposition: form-data; name="another_field";
Content-Type: text/plain
another field data
--boundary_hash
Content-Disposition: form-data; name="filename"; filename="cf_login.png"
Content-Type: image/png
base64 file content
--boundary_hash--
I hope that helps you.
Cheers.
Related
I am trying to CURL tickets/mails with the OTRS software.
The POST request contains of multiple lines like:
-----------------------------21268931210424524295626371432
Content-Disposition: form-data; name="ChallengeToken"
>random-token<
-----------------------------21268931210424524295626371432
Content-Disposition: form-data; name="Action"
AgentTicketEmail
Furthermore the header contains:
Content-Type: multipart/form-data; boundary=---------------------------21268931210424524295626371432
However using a random boundary is not working at all and the server returns an error 500. Does anybody know, how this boundary is generated and validated?
The documentation for the VSCode Rest Client is lacking good explinations. Here is what they give as an example.
POST https://api.example.com/user/upload
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="text"
title
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image"; filename="1.png"
Content-Type: image/png
< ./1.png
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Not sure what the < is for nor the title?
For the boundary part, I recomend read this post.
< It´s a symbol to indicate input stream, the file you want to send needs to be in the same directory as the .rest file that the restclient extension uses.
Quick response: boundary is to define where each pair of fields passed in the form is begins and ends. In your example there are two form fields, text="title" and image=1.png the bytes image sequence.
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image"; filename="1.png"
Content-Type: image/png
< ./1.png
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Other data such as filename="1.png" or Content-Type: image/png indicate additional information that the form loads by default when you select an image with a file type input.
Another example for a field description.
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="description"
All text of description.
------WebKitFormBoundary7MA4YWxkTrZu0gW
this 👇 "Rest Client" snippet works for me (solution from here).
*Note the boundry=MfnBoundry does not use a delimiter, while --MfnBoundry is used to open it, and --MfnBoundry-- is used to close it.
POST http://localhost:3000/api/upload HTTP/1.1
Content-Type: multipart/form-data; boundary=MfnBoundry
--MfnBoundry
Content-Disposition: form-data; name="files"; filename="test.txt"
Content-Type: text/plain
< ./test.txt
--MfnBoundry--
I'm having some trouble uploading an image to an asana task. I get a 200 on the response but when I navigate to the image it is not loading and appears broken.
Here is what my request header looks like:
https://app.asana.com/api/1.0/tasks/<TaskId>/attachments - Http Post
Authorization: Bearer <o-auth token>
Content-Type: multipart/form-data; boundary=ASANA_BOUNDARY
Host: app.asana.com
Accept: */*
Content-Length: 1457324
Fiddler-Encoding: UTF8
Here is what the body of my request looks like:
--ASANA_BOUNDARY
Content-Disposition: form-data; name="file"; filename="koala.jpg"
Content-Type: image/jpeg
<Raw Data>
--ASANA_BOUNDARY--
I'm not really sure why this is giving me a 200 and then no image loads. I formed my request as noted in the documentation.
I am trying to upload an mp4 video to the DailyMotion Upload API. When I send the file content, I receive an error response as below.
Id = 31, Status = RanToCompletion, Method = "{null}",
Result = "{\"error\":\"missing file\",\"seal\":\"4a24a4c8a51771d9d3b8bcd4462e721b\"}"
I am using C# in .net 4.5 and have generated the following calls:
POST [[UPLOAD URL]] HTTP/1.1
Accept: */*
Content-Type: multipart/form-data; boundary="c83ccd81-39f7-4167-a8f1-74ab63eb4219"
Host: upload-01.sv6.dailymotion.com
Content-Length: 9893501
Expect: 100-continue
--c83ccd81-39f7-4167-a8f1-74ab63eb4219
Content-Disposition: form-data; name="file"; filename="TESTFILE.mp4"
Content-Type: application/octet-stream
[[[FILE BINARY]]]
I have successfully uploaded using DailyMotion's CURL sample. Using Fiddler, CURL generates the following:
POST [[UPLOAD URL]] HTTP/1.1
User-Agent: curl/7.33.0
Host: upload-02.sv6.dailymotion.com
Accept: */*
Connection: Keep-Alive
Content-Length: 9893509
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------a49610ec11411f2a
--------------------------a49610ec11411f2a
Content-Disposition: form-data; name="file"; filename="TESTFILE.mp4"
Content-Type: application/octet-stream
[[FILE BINARY]]
The file binary looks good and the only differences I see are the connection and User-Agent headers. Researching the Connection header, it looks like Keep-Alive is unnecessary with HTTP/1.1. I have tried several different User-Agents (Mozilla, CURL, IE) but no change in response.
I appreciate any insight into what is happening.
Thank you!
EDIT Added code:
mediatype = "video/mp4"
name = "file"
filename = "file"
data = {byte[9893291]}
public async Task UploadRequest(string mediatype, string name, string filename, byte[] data)
{
using(HttpClient httpClient = new HttpClient())
{
var requestContent = new MultipartFormDataContent();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
var videoContent = new ByteArrayContent(data);
videoContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
videoContent.Headers.ContentDisposition.Name = "\"file\"";
videoContent.Headers.ContentDisposition.FileName = filename;
videoContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
requestContent.Add(videoContent, name);
Apirequest.HttpRequestMessage.Content = requestContent;
Apirequest.HttpResponseMessage = httpClient.SendAsync(Apirequest.HttpRequestMessage).Result;
Apirequest.HttpResponseMessage.EnsureSuccessStatusCode();
}
}
EDIT Updated following help
POST http://upload-02.nyc.dailymotion.com/upload?uuid=c8542d0a9be73c55fbace1ee4aa1744b&seal=4770461f7f07b4e0c15750d70bc44377 HTTP/1.1
Accept: */*
Content-Type: multipart/form-data; boundary=62fcae82-e1b2-4df3-99c4-90fad29be62d
Host: upload-02.nyc.dailymotion.com
Content-Length: 9893493
Expect: 100-continue
--62fcae82-e1b2-4df3-99c4-90fad29be62d
Content-Disposition: form-data; name="Testfile.mp4"; filename=Testfile.mp4
Content-Type: application/octet-stream
���ftypmp42���mp41mp42isom���wide���mdat!�#h!
�Ќ>��SZa��x�M�SZa��{�!
�ˊ��]C���I��x�8V]�F}=�_Pc1�w�d>��.�!
������hRDq��0�`D�4�QG��A��nn��û�6��ݳ���ι�τpE����s���8)�=����^�ʇ�/�N8k.��]Jr)�z���pTR�
=}.�!
[[FILE GOES ON]]
ðb ’h 'v† )ˆI 7| F3• Ršv _Ê0 kC yDÀ ‚¡] ‰¢ h ŽÂi /
×ÿ ¥ Ó
‘`Q ‘É ’Ö ’K ’Õ ’ÿ\ “ˆŠ “³Â ”"G ”7» +udta #titl ÇTestFile
--62fcae82-e1b2-4df3-99c4-90fad29be62d--
EDIT: A code example of the fix was requested. It is as follows:
videoContent.Headers.ContentDisposition.FileName = "\"" + name + "\"";
After investigation, it looks like the nginx-upload-module that is responsible for handling file uploads on our side doesn't understand quoted content boundaries in HTTP headers, which, according to the RFC1341, is not the standard way of defining boundaries in HTTP anyway. It looks like the C# library you are using to craft your request may be following the standard for the multipart/digest content type instead of the multipart/form-data (cf section 7.2.4). Please check whether this is something you have any control on or not and test again.
Trying to submit a file to the Dropbox through PHP to a Desire2Learn Learning Suite. I don't see a field name documented to contain the file. Am I missing something?
No, you are not missing something. There is no field name for the file for dropbox submissions. Leave the name attribute as an empty string when posting the submission file.
Here’s what a submission request should look like:
POST http://{domain_name}/d2l/api/le/{version}/{org_unit}/dropbox/folders/{folder_id}/submissions/mysubmissions/?x_a={app_id}&x_b={token_id}&x_d={token_sig}&x_c={app_sig}&x_t={time} HTTP/1.1
Content-Type: multipart/mixed; boundary=8cf23611201b7ae
Host: {domain_name}
Content-Length: 775926
--8cf23611201b7ae
Content-Type: application/json
{"Text":"Here you go","HTML":null}
--8cf23611201b7ae
Content-Disposition: form-data; name=""; filename="Jellyfish.jpg"
Content-Type: image/jpeg
{binary data}
--8cf23611201b7ae--