我想实现一个REST API,并且在我的GET请求上需要一个正文.(如下所述:带请求主体的HTTP GET)
是否有http客户端无法发送带有GET请求的正文?Fiddler能够做到这一点,虽然消息框是红色的.
我收到了一个网址.. www.abc.com/details
并要求在此网址上发送我的姓名和电话号码POST
.他们告诉我使用以下键将content-type设置为application/json,将body设置为有效的JSON:
name: name of the user
phone number: phone number of the user
Run Code Online (Sandbox Code Playgroud)
现在我不知道如何发送此请求!会是这样的:
http://www.abc.com/details?method=post&name=john&phonenumber=445566
Run Code Online (Sandbox Code Playgroud)
或者我必须使用java
发送相同的?
请帮忙
我尝试通过node.js版本0.8.14的http模块向某个站点(不是我自己的站点)发出GET请求.这是我的代码(CoffeeScript):
options =
host: 'www.ya.ru'
method: 'GET'
req = http.request options, (res) ->
output = ''
console.log 'STATUS: ' + res.statusCode
res.on 'data', (chunk) ->
console.log 'A new chunk: ', chunk
output += chunk
res.on 'end', () ->
console.log output
console.log 'End GET Request'
req.on 'error', (err) ->
console.log 'Error: ', err
req.end()
Run Code Online (Sandbox Code Playgroud)
我在此操作期间收到以下错误:{[错误:套接字挂断]代码:'ECONNRESET'}.如果我评论错误处理程序我的应用程序已完成以下错误:
events.js:48
throw arguments[1]; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (http.js:1091:15)
at Socket.onend (http.js:1154:27)
at TCP.onread (net.js:363:26)
Run Code Online (Sandbox Code Playgroud)
我试图在互联网上找到解决方案,但仍然没有找到它们.如何解决这个问题?
我正在使用PRG模式来避免多个表单提交.然而,它有一个严重的缺点 - 你不能简单地echo
向用户发出确认消息(显然,用户不会看到该页面,他将被重定向到另一个页面).
这个问题的解决方案是什么?我知道其中两个,但它们似乎都不完美.
http://example.com/?msg=data-saved
.它是无国籍的,所以我认为它非常可靠.但是当用户复制链接,书签等时会产生问题.或许还有其他方法我不知道?会话和URL参数的某些组合?不知道.
你认为最好的方法是什么?哪一个缺点最少?优缺点都有什么?
error-handling session user-interface redirect post-redirect-get
例:
方法: GET
回复:collection
匹配中的所有项目a search term
.
问题:搜索词可能太长,以至于它破坏了Web服务器的最大URL长度.
如何允许极长的搜索条件并保持RESTful状态?
我有REST服务,应该通过GET接收很长的查询.比方说,我想查询具有许多地理坐标的服务,以找出有关所有这些坐标的信息.
1)我的第一个想法是使用长URI并增加 servlet容器的最大URI长度.
它看起来像这样:
GET http://some.test/myresource?query={really big JSON object}
Run Code Online (Sandbox Code Playgroud)
但是由于旧的代理服务器,似乎长度超过2 KB的URI是不可靠的(是吗?).
2)我的解决方法是首先通过POST创建临时资源,并使用此资源的URI作为实际GET请求中的参数.这看起来像这样:
POST http://some.test/temp
Request Body: {really big JSON object}
201 Created Location: http://some.test/temp/12309871
GET http://some.test/myresource?query=http://some.test/temp/12309871
Run Code Online (Sandbox Code Playgroud)
3)使用GET请求的主体.我已经阅读了问题的答案,是否使用GET请求的主体进行查询是一个好主意,并且共识是:不.甚至罗伊菲尔丁说这是一个坏主意.
4)另一种方法可以是将POST解释为"创建查询结果资源"并在请求后删除该资源.但我认为这不是RESTful而是一个坏主意.
有没有更好的方法来处理GET请求的大查询?
我正在使用ASP.NET Core MVC创建一个网站.当我点击某个动作时,我收到此错误:
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
Web.Controllers.ChangeEventsController.Create (Web)
Web.Controllers.ProductsController.CreateChangeEvent (Web)
Run Code Online (Sandbox Code Playgroud)
这就是我在我的ProductsController的index.cshtmlm中定义我的动作的方法:
<a asp-controller="ChangeEvents" asp-action="Create" asp-route-id="@item.Id">Create Change Event</a>
Run Code Online (Sandbox Code Playgroud)
这是我的路线:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
以下是我定义操作的方式:
// ChangeEventsController
[HttpGet("{id}")]
public IActionResult Create(Guid id)
// ProductsController
[HttpGet("{id}")]
public IActionResult CreateChangeEvent(Guid id)
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
更新
感谢@MegaTron的回复,但是我想知道为什么我不能为不同的控制器提供相同的操作路径.如果我有许多控制器,每个创建实体,我觉得你提出的解决方案不会很好地扩展.
目前要向API接口发送参数化GET请求,我正在编写以下代码:
api/master/city/filter?cityid=1&citycode='ny'
Run Code Online (Sandbox Code Playgroud)
但我发现URL长度限制为2,083个字符.
为了避免这种情况,我想在内容体中以json格式发送参数以获取GET请求.
但是,我发现HttpClient的Get方法都不允许发送内容正文.对于POST,我可以看到HttpClient中有一个名为PostAsync的方法允许内容体.
有没有办法为不在URL中的GET请求发送参数以避免URL长度限制?
我必须与从 GET 请求正文中获取参数的 API 进行交互。我知道这可能不是最好的主意,但它是 API 的构建方式。
当我尝试使用 构建查询时XMLHttpRequest
,看起来负载根本没有发送。您可以运行它并查看网络选项卡;请求已发送,但没有正文(在最新的 Chrome 和 Firefox 中测试):
const data = {
foo: {
bar: [1, 2, 3]
}
}
const xhr = new XMLHttpRequest()
xhr.open('GET', 'https://my-json-server.typicode.com/typicode/demo/posts')
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
xhr.send(JSON.stringify(data))
Run Code Online (Sandbox Code Playgroud)
诸如 axios 之类的库是基于 XMLHttpRequest 构建的,因此它们也无法正常工作...
有没有办法在 JavaScript 中实现这一点?
是不是将REST风格的方法与GET请求一起传递请求体?
例如,在Elasticsearch中过滤一些信息
curl localhost:9200/megacorp/employee/_search -d '{"query" : {"filtered" : {"filter" : {"range" : {"age" : { "gt" : 30 }}},"query" : {"match" : {"last_name" : "smith"}}}}}'
Run Code Online (Sandbox Code Playgroud)
一些工具甚至被设计为避免GET请求中的请求体(如邮递员)