如何为解析错误自定义HTTP 400响应? [英] How can I customize HTTP 400 responses for parse errors?

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

问题描述

我编写了REST API服务,该服务要求所有响应均为JSON。但是,当Go HTTP请求解析器遇到错误时,它将返回400作为纯文本响应,而无需调用我的处理程序。示例:

I've written a 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

请注意无效的Authorization标头。当然,400是正确的答案,但是当然是文本/纯文本。有什么方法可以配置Go http解析器以使用自定义错误响应媒体类型和主体?

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?

推荐答案

您不能。您可以在net / http源中找到它,只有在请求格式错误时才会发生:

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

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

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

401、403、404、500错误,您可以使用json进行响应,但错误的请求或错误的标头(太长,格式错误)在server.go中处理。

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.

尽管在考虑,因此您唯一的解决方案是打补丁stdlib源(我不建议这样做)。但是,由于此错误仅在客户犯了一个错误并且请求格式错误时才出现,因此可能不是一个大问题。产生文字回应的原因是,浏览器或类似客户端(例如不带-v的curl)不会仅看到空的回应。您可以在应用程序前面放置一个像nginx这样的代理,但是由于它是一个不好的请求,因此您永远也看不到该请求,您的代理会处理该请求。

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.

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

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.

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

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