尝试在Ruby中复制移动应用程序POST请求,收到502网关错误

尝试在Ruby中复制移动应用程序POST请求,收到502网关错误,ruby,charles-proxy,Ruby,Charles Proxy,我正在尝试使用Ruby在iPhone应用程序中自动执行我可以手动执行的操作,但当我这样做时,我得到了一个502错误网关 使用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 Ac

我正在尝试使用Ruby在iPhone应用程序中自动执行我可以手动执行的操作,但当我这样做时,我得到了一个502错误网关

使用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错误网关

我注意到,我认为解决方案的关键是,在移动应用程序发出的POST请求中,内容长度为66。但是,带有未编辑用户ID的字符串“invitedUserId=REDACTED&source=PROFILE\u INVITATION”的长度只有46

我是否缺少另一个格式为“¶m=value”且长度为20的表单变量?还是我错过了什么


提前谢谢你

这可能与您发送的身体长度没有直接关系

我在这里可能看到两个问题:

  • 502错误:您的
    uri.host
    端口是否正确?502错误意味着服务器端有问题。还可以尝试删除
    主机
    标题
  • 正文内容未压缩
您正在定义一个头
内容编码: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)
需要“gzip”
@标题_post={
# ... 
}
http=Net::http.new(uri.host,uri.port)
路径='/1.1/user/-/friends/investments'
data=“invitedUserId=redact&source=PROFILE\u邀请”
#实例化一个新的gzip缓冲区
gzip=Zlib::GzipWriter.new(StringIO.new)
#附加数据

gzip让你的生活更轻松。使用HTTParty,就像大多数Ruby开发者所做的那样: