ruby - 尝试在 Ruby 中复制移动应用程序 POST 请求,出现 502 网关错误
问题描述
我正在尝试自动化我可以使用 Ruby 在 iPhone 应用程序中手动执行的操作,但是当我这样做时,我收到 502 bad gateway 错误。
使用 Charles Proxy 我收到了 iPhone 应用程序发出的请求:
POST /1.1/user/-/friends/invitations HTTP/1.1
Host: redacted.com
Accept-Locale: en_US
Accept: */*
Authorization: Bearer REDACTED
Content-Encoding: gzip
Accept-Encoding: br, gzip, deflate
Accept-Language: en_US
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 66
Connection: keep-alive
X-App-Version: 814
invitedUserId=REDACTED&source=PROFILE_INVITATION
我在 Ruby 中编写了以下代码来发送相同的请求:
@header_post = {
"Host" => "redacted.com",
"Accept-Locale" => "en_US",
"Accept" => "*/*",
"Authorization" => "Bearer REDACTED",
"Content-Encoding" => "gzip",
"Accept-Encoding" => "br, gzip, deflate",
"Accept-Language" => "en_US",
"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8",
"Connection" => "keep-alive",
"X-App-Version" => "814"
}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
path = '/1.1/user/-/friends/invitations'
data = "invitedUserId=REDACTED&source=PROFILE_INVITATION"
resp, data = http.post(path, data, @header_post)
不幸的是,运行此代码时出现 502 Bad Gateway Error。
我注意到的一件事是我认为这里解决方案的关键是,在移动应用程序发出的 POST 请求中,内容长度为 66。但字符串“invitedUserId=REDACTED&source=PROFILE_INVITATION”的长度与未编辑的 userId只有46岁。
我是否缺少另一个格式为“¶m=value”且长度为 20 的表单变量?还是我错过了其他东西?
先感谢您!
解决方案
这可能与您发送的正文长度没有直接关系。
我在这里看到可能有 2 个问题:
- 502 错误:你的
uri.host
正确port
吗?502 错误意味着服务器端有问题。也可以尝试删除Host
标题。 - 正文内容未压缩
您正在定义一个标头Content-Encoding: gzip
,但您没有压缩数据(Net::Http
不会自动执行此操作)。
尝试类似的东西:
require "gzip"
@header_post = {
# ...
}
http = Net::HTTP.new(uri.host, uri.port)
path = '/1.1/user/-/friends/invitations'
data = "invitedUserId=REDACTED&source=PROFILE_INVITATION"
# instanciate a new gzip buffer
gzip = Zlib::GzipWriter.new(StringIO.new)
# append your data
gzip << data
# get the gzip body and use it in your request
body = gzip.close.string
resp, data = http.post(path, body, @header_post)
或者,服务器可能正在接受非压缩内容。您只需Content-Encoding
从原始代码中删除错误即可尝试。
但是,如果这是唯一的错误,则服务器不应发送 502 而是 4xx 错误。所以我猜想上面建议的uri配置还有另一个问题。
推荐阅读
- c++ - 如何实现回调系统
- oauth-2.0 - API 到 API - 使用服务帐户
- google-tag-manager - 如何使用 Google 跟踪代码管理器跟踪特定链接?
- javascript - 根据当前时间突出显示表格行
- python - 无法将字符串转换为浮动 sklearn
- c# - 天真的 TensorFlow 模型问题,如何将输入和输出匹配到另一个模型
- reactjs - react-dnd 的 useDrag 方法,无法在 typescript 中获取 isDragging
- css - 使用 css 缩放只会留下空白
- python - 我可以在python中获取函数输入吗?
- python - sklearn 和 statsmodels 的逻辑回归结果不匹配