json 如何使用浏览器进行基本的 REST API 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29105007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:47:53  来源:igfitidea点击:

How to make basic REST API calls using a browser

jsonapiresthttp

提问by Daiwik Daarun

I am trying to get started with REST API calls by seeing how to format the API calls using a browser. Most examples I have found online use SDKs or just return all fields for a request.

我正在尝试通过查看如何使用浏览器格式化 API 调用来开始使用 REST API 调用。我在网上找到的大多数示例都使用 SDK,或者只是返回请求的所有字段。

For example, I am trying to use the Soundcloud APIto view track information.

例如,我正在尝试使用Soundcloud API查看曲目信息

To start, I've made a simple request in the browser as follows http://api.soundcloud.com/tracks/13158665.json?client_id=31a9f4a3314c219bd5c79393a8a569ecwhich returns a bunch of info about the track in JSON format

首先,我在浏览器中发出了一个简单的请求,如下所示http://api.soundcloud.com/tracks/13158665.json?client_id=31a9f4a3314c219bd5c79393a8a569ec,它以 JSON 格式返回一堆关于赛道的信息

(e.g. {"kind":"track","id":13158665,"created_at":"2011/04/06 15:37:43 ...})

(例如{"kind":"track","id":13158665,"created_at":"2011/04/06 15:37:43 ...}

Is it possible to only to get returned the "created_at" value using the browser? I apologize if this question is basic, but I don't know what keywords to search online. Links to basic guides would be nice, although I would prefer to stay out of using a specific SDK for the time being.

是否可以只使用浏览器返回“created_at”值?如果这个问题很基本,我深表歉意,但我不知道在线搜索哪些关键字。基本指南的链接会很好,尽管我暂时不想使用特定的 SDK。

回答by Thierry Templier

In fact, it's really hard to answer such question since it depends on the Web APIs. I mean if the API supports to return only a subset of fields, you could but if not, you will receive all the content. From what I saw on the documentation, it's not possible. The filters only allow you to get a subset of elements and not control the list of returned fields within elements.

事实上,这个问题真的很难回答,因为它取决于 Web API。我的意思是,如果 API 支持仅返回字段的子集,您可以,但如果不支持,您将收到所有内容。从我在文档中看到的,这是不可能的。过滤器只允许您获取元素的子集,而不能控制元素内返回的字段列表。

Notice that you have a great application to execute HTTP requests (and also REST) in Chrome: Postman. This allows to execute all HTTP methods and not only GET ones and controls the headers and sent content and also see what is received back.

请注意,您有一个很棒的应用程序可以在 Chrome 中执行 HTTP 请求(以及 REST):Postman。这允许执行所有 HTTP 方法,而不仅仅是 GET 方法,并控制标头和发送的内容,还可以查看收到的内容。

If you use Firefox, Firebugprovides a similar thing.

如果您使用 Firefox,Firebug提供了类似的功能。

To finish, you could have a look at this link to find out hints about the way Web APIs work and are designed: https://templth.wordpress.com/2014/12/15/designing-a-web-api/.

最后,您可以查看此链接以了解有关 Web API 工作和设计方式的提示:https: //templth.wordpress.com/2014/12/15/designing-a-web-api/

Hope it helps you and I answered you question, Thierry

希望它对你有帮助,我回答了你的问题,蒂埃里

回答by Abhinav Rawat

you can install DHC client app in your chrome and send request like put or get

您可以在 chrome 中安装 DHC 客户端应用程序并发送请求,如 put 或 get

回答by GMLewisII

Straight from the browser bar you can utilize REST endpoints that respond to a GET message. That is what you are doing when you hit that URI, you are sending an HTTP GET message to that server and it is sending back a JSON.

您可以直接从浏览器栏中使用响应 GET 消息的 REST 端点。这就是您在点击该 URI 时所做的事情,您正在向该服务器发送 HTTP GET 消息,并且它正在发送回 JSON。

You are not always guaranteed a JSON, or anything when hitting a known REST endpoint. What each endpoint returns when hit with a GET is specific to how it was built. In that case, it is built to return a JSON, but some may return an HTML page. In my personal experience, most endpoints that utilize JSON returns expect you to process that object in a computer fashion and don't give you a lot of options to get a specific field of the JSON. Here is a good linkon how to process JSON utilizing JavaScript.

在访问已知的 REST 端点时,您并不总是能保证 JSON 或任何内容。当使用 GET 命中时,每个端点返回的内容特定于它的构建方式。在这种情况下,它被构建为返回一个 JSON,但有些可能返回一个 HTML 页面。根据我的个人经验,大多数使用 JSON 返回的端点都希望您以计算机方式处理该对象,并且不会为您提供很多选项来获取 JSON 的特定字段。这是一个关于如何使用 JavaScript 处理 JSON的好链接

You can utilize REST clients (such as the Advanced REST Client for Chrome) to craft HTTP POST and PUT if a specific REST endpoint has the functionality built in to receive data and do something with it. For example, a lot of wiki style REST endpoints will allow you to create a page with a specifically crafted HTTP POST with either specific header information, URI parameters or a JSON as part of it.

如果特定的 REST 端点具有内置的接收数据和处理数据的功能,您可以利用 REST 客户端(例如Chrome高级 REST 客户端)来制作 HTTP POST 和 PUT。例如,许多 wiki 风格的 REST 端点将允许您使用特制的 HTTP POST 创建一个页面,其中包含特定的标头信息、URI 参数或 JSON 作为其中的一部分。