如何使用Power Query的Web.Contents来打印多部分/表单数据 [英] How to POST a multipart/form-data using Power Query's Web.Contents

查看:963
本文介绍了如何使用Power Query的Web.Contents来打印多部分/表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在电力查询中,我可以使用Web.Contents函数从Web下载数据,但是有一个api需要以下列格式包含多部分/表单数据。



__ rdxml=< *某些数据*>



那么你如何使用Web.Contents函数?



我尝试了,

 。 .. 
PostContent =__rdxml =< *一些数据*>,
源Web.Contents(url,Content = Text.ToBinary(PostContent))
...

但服务器响应与 400 Bad Request p>

我用Fiddler检查了原始请求,似乎请求不是以 content-type = multipart / form-data 头。



我尝试手动添加内容类型标题与 content-type = multipart / form-data ,但是也不行。



任何想法?

解决方案

multipart / form-data是一个相当复杂的编码,需要一堆MIME特定的头文件。我会首先尝试看看是否可以使用application / x-www-form-urlencoded:

  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

编辑:我想出了一个使用multipart / form-data的例子与电力查询。这是在 https://gist.github.com/CurtHagenlocher/b21ce9cddf54e3807317


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

这篇关于如何使用Power Query的Web.Contents来打印多部分/表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆