如何从HTTP POST请求(到另一个域)获取JSON

时间:2022-10-18 07:41:47

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

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

Authenticated Sessions (taken from here)

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

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

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

  • URL: http://stage.amee.com/auth (this is not my domain)
  • 网址:http://stage.amee.com/auth(这不是我的域名)

  • Method: POST
  • Request format: application/x-www-form-urlencoded
  • 请求格式:application / x-www-form-urlencoded

  • Response format: application/xml, application/json
  • 响应格式:application / xml,application / json

  • Response code: 200 OK
  • 响应代码:200 OK

  • Response body: Details of the authenticated user, including API version.
  • 响应正文:经过身份验证的用户的详细信息,包括API版本。

  • Extra data: "authToken" cookie and header, containing the authentication token that should be used for subsequent calls.

    额外数据:“authToken”cookie和标头,包含应该用于后续调用的身份验证令牌。

    Parameters: username / password

    参数:用户名/密码

Example

Request

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

POST / auth HTTP / 1.1接受: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

HTTP / 1.1 200 OK Set-Cookie:authToken = 1KVARbypAjxLGViZ0Cg + UskZEHmqVkhx / Pm ......; authToken:1KVARbypAjxLGViZ0Cg + UskZEHmqVkhx / PmEvzkPGp ... == Content-Type:application / xml;字符集= 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.

我尝试过jQuery,但它似乎与XSS有关。实际的代码片段将不胜感激。

p.s.

All I was looking for was WebClient class in C#

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

2 个解决方案

#1


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

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

#2


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

我正在使用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

#1


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

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

#2


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

我正在使用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