尝试使用uri库以正确的格式为eventbrite批处理终结点生成json,但未从uri库获得正确的编码。

url = URI.escape('{"method":"GET", "relative_url":"users/me"},{"method":"GET", "relative_url":"users/me/owned_events/"},{"method":"GET", "relative_url":"users/me/owned_events/?page=2"}')

=>%7B%22method%22:%22GET%22,%20%22relative_url%22:%22users/me%22%7D,
%7B%22method%22:%22GET%22,%20%22relative_url%22:%22users/me/owned_events/%22%7D,
%7B%22method%22:%22GET%22,%20%22relative_url%22:%22users/me/owned_events/?page=2%22%7D

这是所需的编码:
%5B+++++%7B%22method%22%3A%22GET%22%2C+%22relative_url%22%3A%22users%2Fme
%22%7D%2C+++++%7B%22method%22%3A%22GET%22%2C+%22relative_url%22%3A
%22users%2Fme%2Fowned_events%2F%22%7D%2C+++++%7B%22method%22%3A%22GET
%22%2C+%22relative_url%22%3A%22users%2Fme%2Fowned_events%2F%3Fpage%3D2%22%7D+%5D

最佳答案

我相信您忘记将json对象括在数组中(请注意方括号):
cgi.escape('[{“method”:“获取”,
“相对url”:“users/me”},{“method”:“获取”,
“relative_url”:“users/me/owned_events/”},{“method”:“get”,
“relative_url”:“用户/me/owned_事件/?页面= 2“}”
结果:
%5B%5B%7B%22方法%22%3A%22%3A%22%3A%22%3A%22%22用户%2%22%7D%2C%2C%7B%22%22方法%22%3A%22%22%3A%22%3A%22%22%22%22%22%22%22%22%22%22%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%5B%22%5B%5B%5B%22%5B%5B%5B%丁
这和你想要的结果是一样的,给或者留空格。
顺便说一下,我不知道你为什么要用uri。据我所知,uri应该是eventbriteapi.com/v3/batch的。基于它们的documentation,您发送的负载似乎应该通过post请求发送到此uri。对于有效载荷的编码,应使用CGIBase64

10-06 07:25