简体   繁体   中英

How to POST a multipart/form-data using Power Query's Web.Contents

In Power Query, I can download data from Web using the Web.Contents function, but there's an api that required the request to contains multipart/form data in the following format

"__rdxml"=<*Some data*>

So how do you do this using the Web.Contents function?

I tried, doing

...
PostContent = "__rdxml=<*Some data*>",
Source Web.Contents(url,Content=Text.ToBinary(PostContent))
...

But server response with 400 Bad Request .

I checked the raw request with Fiddler, it seem like the request is not sending with content-type=multipart/form-data header.

I tried manually adding the content-type header with content-type=multipart/form-data , but that doesn't work either. Same 400 Bad Request in the response.

Any idea?

multipart/form-data is a fairly complicated encoding, requiring a bunch of MIME-specific headers. I would first try to see if you can use application/x-www-form-urlencoded instead:

let
    actualUrl = "http://some.url",
    record = [__rdxml="some data"],
    body = Text.ToBinary(Uri.BuildQueryString(record)),
    options = [Headers =[#"Content-type"="application/x-www-form-urlencoded"], Content=body],
    result = Web.Contents(actualUrl, options)
in
    result

EDIT: I've come up with an example of using multipart/form-data with Power Query. It's at https://gist.github.com/CurtHagenlocher/b21ce9cddf54e3807317

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM