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

二次信任 提交于 2019-12-01 07:54:25

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/28361628/how-to-post-a-multipart-form-data-using-power-querys-web-contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!