Ryan Seddon
hacking
Changing content type of a file in a FormData request with cURL
The other day I had to test an API response based on a file having the incorrect Content-Type of a FormData PUT request.
After some digging in cURL I found the -F
& --form
flag to set the file and its content type. If you don’t specify the content type it will be inferred from the files extension.
curl -X PUT https://example.com/api/thing --form '[email protected];type=text/plain'
Using --form
we pass the form data with the following format:
[name]=@[path/to/file];type=[override content-type]
Given your endpoints validation cURL will return whatever response it gets from the API.
Trace ascii output
curl -X PUT https://example.com/api/thing --form '[email protected];type=text/plain' --trace-ascii -
If you want to see a nicely formatted output you can pass the --trace-ascii -
flag which will show the request body as well as the response and all headers. The extra -
means it will output to stdout
rather than write to a file.
curl -X PUT https://example.com --form '[email protected];type=text/plain' --trace-ascii -
== Info: Rebuilt URL to: https://example.com/
== Info: Trying 93.184.216.34...
== Info: Connected to example.com (93.184.216.34) port 80 (#0)
=> Send header, 267 bytes (0x10b)
0000: PUT / HTTP/1.1
0010: Host: example.com
0023: User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; T
0063: rident/5.0)
0070: Accept: */*
007d: Referer:
0088: Content-Length: 472
009d: Expect: 100-continue
00b3: Content-Type: multipart/form-data; boundary=--------------------
00f3: ----25e7f48e0eecccf4
0109:
<= Recv header, 23 bytes (0x17)
0000: HTTP/1.1 100 Continue
=> Send data, 139 bytes (0x8b)
0000: --------------------------25e7f48e0eecccf4
002c: Content-Disposition: form-data; name="csv"; filename="stocks.csv
006c: "
006f: Content-Type: text/plain
0089:
=> Send data, 285 bytes (0x11d)
0000: Date,Open,High,Low,Close,Volume,Adj Close.2016-07-19,305.00,305.
0040: 929993,265.00,288.00,103500,288.00.2016-07-18,286.73999,308.25,2
0080: 85.50,299.00,118000,299.00.2016-07-15,262.019989,267.26001,259.0
00c0: 40009,267.209991,28100,267.209991.2016-07-14,244.220001,246.00,2
0100: 36.800003,243.75,38400,243.75
=> Send data, 48 bytes (0x30)
0000:
0002: --------------------------25e7f48e0eecccf4--
<= Recv header, 33 bytes (0x21)
0000: HTTP/1.1 405 Method Not Allowed
<= Recv header, 31 bytes (0x1f)
0000: Cache-Control: max-age=604800
<= Recv header, 37 bytes (0x25)
0000: Date: Wed, 20 Jul 2016 04:30:16 GMT
<= Recv header, 40 bytes (0x28)
0000: Expires: Wed, 27 Jul 2016 04:30:16 GMT
<= Recv header, 27 bytes (0x1b)
0000: Server: EOS (lax004/2812)
<= Recv header, 19 bytes (0x13)
0000: Content-Length: 0
== Info: HTTP error before end of send, stop sending
<= Recv header, 2 bytes (0x2)
0000:
== Info: Closing connection 0
Hope this helps someone else in the same situation.