如何从 HTTP POST 请求(到另一个域)返回 JSON

发布于 2024-07-25 14:31:47 字数 1313 浏览 2 评论 0原文

我正在尝试在网站上使用 API,这是手册的一部分:

经过身份验证的会话(摘自 此处

要创建经过身份验证的会话,您需要从“/auth”API 资源请求 authToken。

  • URL:http://stage.amee.com/auth (这不是我的域)
  • 方法:POST
  • 请求格式:application/x-www-form-urlencoded
  • 响应格式:application/xml、application/json
  • 响应码:200 OK
  • 响应正文:认证用户的详细信息,包括 API 版本。
  • 额外数据:“authToken”cookie 和标头,包含 身份验证令牌应该是 用于后续调用。

    参数:用户名/密码

示例

请求

POST /auth HTTP/1.1
接受:application/xml
内容类型:application/x-www-form-urlencoded

username=my_username&password=my_password

响应

HTTP/1.1 200 OK 设置 Cookie: authToken=1KVARbypAjxLGViZ0Cg+UskZEHmqVkhx/Pm...;
authToken: 1KVARbypAjxLGViZ0Cg+UskZEHmqVkhx/PmEvzkPGp...==
内容类型:application/xml; charset=UTF-8

问题:

我如何让它工作?

我尝试了 jQuery,但它似乎存在 XSS 问题。 实际的代码片段将不胜感激。

ps

我所寻找的是 WebClient C# 中的类

I'm trying to use the API on a website, here's the part of the manual:

Authenticated Sessions (taken from here)

To create an authenticated session, you need to request an authToken from the '/auth' API resource.

  • URL: http://stage.amee.com/auth (this is not my domain)
  • Method: POST
  • Request format: application/x-www-form-urlencoded
  • Response format: application/xml, application/json
  • Response code: 200 OK
  • Response body: Details of the authenticated user, including API
    version.
  • Extra data: "authToken" cookie and header, containing the
    authentication token that should be
    used for subsequent calls.

    Parameters: username / password

Example

Request

POST /auth HTTP/1.1
Accept: application/xml
Content-Type: application/x-www-form-urlencoded

username=my_username&password=my_password

Response

HTTP/1.1 200 OK
Set-Cookie: authToken=1KVARbypAjxLGViZ0Cg+UskZEHmqVkhx/Pm...;
authToken: 1KVARbypAjxLGViZ0Cg+UskZEHmqVkhx/PmEvzkPGp...==
Content-Type: application/xml; charset=UTF-8

QUESTION:

How do I get that to work?

I tried jQuery, but it seems to have problem with XSS. Actual code snippet would be greatly appreciated.

p.s.

All I was looking for was WebClient class in C#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城已无爱 2024-08-01 14:31:47

您需要将 application/json 放入 Accept 标头中,这告诉服务器您希望它以该格式(而不是 xml)响应。

You need to put application/json in your Accept header, this tells the server you want it to respond in that format - not xml.

执妄 2024-08-01 14:31:47

我正在使用 Rails 从 stage.amee.com/auth 中提取与上面提到的相同的身份验证令牌 cookie。 在我创建并定制正确的请求对象之前,我进行了一些实验,该对象返回 200 OK,并将 authtoken 作为 cookie。 我还没有找到读取请求对象的有效方法,否则我会准确地发布它的样子。 这是应用程序控制器中的我的 ruby​​ 代码

#define parameters
uri=URI.parse('http://stage.amee.com')
@path = '/auth'
@login_details = 'username=your_username&password=your_password'
@headers = {'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json'}

#create request object
req = Net::HTTP.new(uri.host, uri.port)

#send the request using post, defining the path, body and headers
resp, data = req.post(@path, @login_details, @headers)

#print response details to console
puts "response code = " << resp.code
puts "response inspect = " << resp.inspect
resp.each do |key, val| 
  puts "response header key : " + key + " = " + val 
end 
puts "data: " + data

I am using rails to extract the same authentication token cookie from stage.amee.com/auth as mentioned above. it took a bit of experimentation before I created and customised the correct request object that returned a 200 OK, with the authtoken as a cookie. i haven't found an effective method of reading the request object or I would post exactly what it looks like. here is my ruby code from the app's controller

#define parameters
uri=URI.parse('http://stage.amee.com')
@path = '/auth'
@login_details = 'username=your_username&password=your_password'
@headers = {'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json'}

#create request object
req = Net::HTTP.new(uri.host, uri.port)

#send the request using post, defining the path, body and headers
resp, data = req.post(@path, @login_details, @headers)

#print response details to console
puts "response code = " << resp.code
puts "response inspect = " << resp.inspect
resp.each do |key, val| 
  puts "response header key : " + key + " = " + val 
end 
puts "data: " + data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文