Skip to content

Instantly share code, notes, and snippets.

@CurtHagenlocher
Created July 20, 2015 20:18
Show Gist options
  • Select an option

  • Save CurtHagenlocher/b21ce9cddf54e3807317 to your computer and use it in GitHub Desktop.

Select an option

Save CurtHagenlocher/b21ce9cddf54e3807317 to your computer and use it in GitHub Desktop.

Revisions

  1. CurtHagenlocher created this gist Jul 20, 2015.
    36 changes: 36 additions & 0 deletions multipart.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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