如何为解析错误定制Golang http 400响应? [英] How can I customize Golang http 400 responses for parse errors?

查看:2275
本文介绍了如何为解析错误定制Golang http 400响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个REST API服务,要求所有响应都是JSON。但是,当Go http请求解析器遇到错误时,它将以纯文本响应的形式返回400,而不会调用我的处理程序。例子:

 > curl -i -H'Authorization:Basic hi 
there''http:// localhost:8080 / test'-v
* Trying :: 1 ...
* TCP_NODELAY set
*连接到本地主机(:: 1)端口8080(#0)
> GET / test HTTP / 1.1
>主机:localhost:8080
> User-Agent:curl / 7.54.0
>接受:* / *
>授权:基本hi
>有
>
< HTTP / 1.1 400错误请求
HTTP / 1.1 400错误请求
<内容类型:text / plain; charset = utf-8
Content-Type:text / plain; charset = utf-8
<连接:关闭
连接:关闭

<
*关闭连接0

请注意无效的授权标头。当然,400是正确的回应,但它当然是文本/纯文本。有没有一些方法可以配置Go HTTP解析器使用自定义错误响应媒体类型和正文?

解决方案

你不能。你可以在net / http源中找到它,它只发生在请求格式错误时: /blob/master/src/net/http/server.go#L1744rel =nofollow noreferrer> https://github.com/golang/go/blob/master/src/net/http/server.go# L1744



我认为您的问题可能是您在curl中添加的标题中的新行吗?



401,403,404,500错误你可以用json响应,但在server.go内处理不好的请求或错误的标题(太长,格式错误)。


目前没有办法拦截这些错误,虽然它在考虑事项,所以你唯一的解决方案就是修补stdlib源代码(我不建议这样做)。但是,由于此错误仅在客户发生错误并且请求格式错误时才会显示,所以这可能不是一个大问题。文本响应的原因是,浏览器或类似的客户端(如没有-v的curl)不会看到空的响应。你可以在你的应用程序前放置一个像nginx这样的代理,但是你永远不会看到这个请求,因为这是一个错误的请求,你的代理会处理它。

可能你可以在前面使用像nginx这样的代理来完成它,但是如果你设置了一个特定的静态错误页面来为它提供400个错误并服务您指定的400.json文件?这是我能想到的唯一解决方案。像这样的指令可能适用于nginx:

  error_page 400 /400.json; 

如果您想自定义这些错误,也许可以为链接的问题添加评论让他们知道你有这个具体问题。

I've written an REST API service that requires that all responses be JSON. However, when the Go http request parser encounters an error, it returns 400 as a plain text response without ever calling my handlers. Example:

> curl -i -H 'Authorization: Basic hi    
there' 'http://localhost:8080/test' -v
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /test HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Authorization: Basic hi
> there
> 
< HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
< Content-Type: text/plain; charset=utf-8
Content-Type: text/plain; charset=utf-8
< Connection: close
Connection: close

< 
* Closing connection 0

Note the invalid Authorization header. Of course 400 is the proper response, but it's text/plain, of course. Is there some way to configure the Go http parser to use custom error response media types and bodies?

解决方案

You can't. You can find this in net/http source, it only happens if the request was malformed:

https://github.com/golang/go/blob/master/src/net/http/server.go#L1744

I think your problem might be a new line in the header you're adding in curl?

401, 403, 404, 500 errors you'll be able to respond with json, but bad requests or bad headers (too long, malformed) are handled within server.go.

There is at present no way to intercept such errors though it is under consideration, so your only solution in go would be to patch the stdlib source (I don't recommend this). However, since this error only presents if the client has made a mistake and the request is malformed, it's probably not a huge problem. The reason for the text response is so that a browser or similar client (like curl without -v) doesn't just see an empty response. You could put a proxy like nginx in front of your app but then you'd never see the request either as it is a bad request, your proxy would handle it.

Possibly you'd be able to do it with a proxy like nginx in front though if you set a specific static error page for it to serve for 400 errors and serve a 400.json file that you specify? That's the only solution I can think of. A directive something like this might work for nginx:

error_page 400 /400.json;

If you'd like to be able to customise these errors, perhaps add a comment to the issue linked to let them know you had this specific problem.

这篇关于如何为解析错误定制Golang http 400响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆