如何从 HTTP POST 请求(到另一个域)返回 JSON
我正在尝试在网站上使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将 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.
我正在使用 Rails 从 stage.amee.com/auth 中提取与上面提到的相同的身份验证令牌 cookie。 在我创建并定制正确的请求对象之前,我进行了一些实验,该对象返回 200 OK,并将 authtoken 作为 cookie。 我还没有找到读取请求对象的有效方法,否则我会准确地发布它的样子。 这是应用程序控制器中的我的 ruby 代码
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