Created
July 20, 2015 20:18
-
-
Save CurtHagenlocher/b21ce9cddf54e3807317 to your computer and use it in GitHub Desktop.
Using multipart/form-data with Power Query. This assumes that any files being uploaded are textual; changing this to support arbitrary binary data is possible, but wasn't interesting to me.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
Web.MultiPartPost = (url as text, parts as record) as binary => | |
let | |
unique = Text.NewGuid(), | |
boundary = "--" & unique, | |
crlf = "#(cr)#(lf)", | |
item = (name, value) => | |
let | |
filename = Value.Metadata(value)[name]?, | |
contentType = Value.Metadata(value)[type]?, | |
line1 = "Content-Disposition: form-data; name=""" & name & """" & | |
(if filename = null then "" else "; filename=""" & filename & """"), | |
line2 = if contentType = null then {} else { "Content-Type: " & contentType }, | |
lines = { boundary, line1 } & line2 & { "", value } | |
in | |
Text.Combine(lines, crlf) & crlf, | |
body = Text.Combine(List.Transform(Record.FieldNames(parts), each item(_, Record.Field(parts, _)))) & boundary & "--" & crlf | |
in | |
Web.Contents(url, [ | |
Headers=[#"Content-Type"="multipart/form-data; boundary=" & unique], | |
Content=Text.ToBinary(body) | |
]), | |
fileContents = | |
"{""code"": ""15315""} | |
{""code"": ""526133""} | |
{""code"": ""43011""} | |
{""code"": ""83988""} | |
{""code"": ""182347""}", | |
parts = [ | |
id = "c9c43e63-9372-441c-9ab7-c5e66d7da371", | |
file = fileContents meta [name="upload.txt", type="text/plain"], | |
locale = "en-US" | |
], | |
Result = Web.MultiPartPost("http://foo.com/bar/baz", parts) | |
in | |
Result |
What is the purpose of creating filecontents when we are passing the file? should it not pick data from the file?
What is the purpose of creating filecontents when we are passing the file? should it not pick data from the file?
You could get the contents any way you wanted -- File.Contents, AzureStorage.BlobContents, etc.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script really educates me thank you! Since I am new to Power Query I wonder if fileContents part can be omitted? If my text file contains hundreds of rows would I need to list it all line by line? Thank you for your advice :)