提取返回400错误请求

时间:2019-02-23 17:44:13

标签: javascript json

我正在尝试对自己的服务器进行提取请求。错误500理想地是我想要的,因为这意味着请求已成功发送到服务器,并且我的服务器端代码未实现,因此我希望会有HTTP 500响应。

但是,当我放入另一个变量时,它然后返回错误400,这意味着请求的格式不正确,所以我想它甚至没有到达服务器?

我不明白为什么更改变量会导致浏览器说请求的格式不正确(400)。

返回错误500:

fetch("http://example.com/webpush/save_information", {"credentials":"include","headers":{"accept":"*/*","accept-language":"en-US,en;q=0.9","alexatoolbar-alx_ns_ph":"AlexaToolbar/alx-4.0.3","cache-control":"no-cache","pragma":"no-cache"},"referrer":"http://example.com/","referrerPolicy":"no-referrer-when-downgrade","body":JSON.stringify({'test':'1'}),"method":"POST","mode":"cors"});

返回错误400:

fetch("http://example.com/webpush/save_information", {"credentials":"include","headers":{"accept":"*/*","accept-language":"en-US,en;q=0.9","alexatoolbar-alx_ns_ph":"AlexaToolbar/alx-4.0.3","cache-control":"no-cache","pragma":"no-cache"},"referrer":"http://example.com/","referrerPolicy":"no-referrer-when-downgrade","body":JSON.stringify({'browser':'chrome'}),"method":"POST","mode":"cors"});

有人能发现差异吗?

编辑:

显然,它是基于注释在服务器端生成的,因此我正在向问题中添加信息。我继续前进,并从控制台中获取了请求返回错误400的完整信息和完整信息:

curl 'https://example.com/webpush/save_information' -H 'origin: https://example.com' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-US,en;q=0.9' -H 'alexatoolbar-alx_ns_ph: AlexaToolbar/alx-4.0.3' -H 'cookie: __cfduid=d558e24311af0551aa5e71c5182a5973c1543351195; _ga=GA1.2.252947937.1543425255; _gid=GA1.2.1592442174.1550785627; gsScrollPos-4659=0; gsScrollPos-4711=; gsScrollPos-4678=' -H 'pragma: no-cache' -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36' -H 'content-type: application/json' -H 'accept: */*' -H 'cache-control: no-cache' -H 'authority: example.com' -H 'referer: https://example.com/' --data-binary '{"status_type":"subscribe","subscription":{"endpoint":"https://fcm.googleapis.com/fcm/send/fiGP6rOnDwE:APA91bEYi2Oydq9c5zMug2QeZK3VDd089-7-YylSnUFqpqc3WlFrZT2tkejzROplb0SHCMA2Tc9Xq0ujhpUa3S-8yWpONgNJ4tFEkyy0m3jcvi3BP2FtNp6_pbdgtLP2nwbuDjkHpc8r","expirationTime":null,"keys":{"p256dh":"BGSJ9xrSKmkTKXjMGYfFAWZ2c_Cj44QJ1k5HkvsDPF-8iphCdQAaSS_ob3G7MOSzsEO-L7fUVs7US21MfuBV-4c","auth":"v8RL7EgDnBlSXnvT7PhvVw"}},"browser":"chrome"}' --compressed

fetch("https://example.com/webpush/save_information", {"credentials":"include","headers":{"accept":"*/*","accept-language":"en-US,en;q=0.9","alexatoolbar-alx_ns_ph":"AlexaToolbar/alx-4.0.3","cache-control":"no-cache","content-type":"application/json","pragma":"no-cache"},"referrer":"https://example.com/","referrerPolicy":"no-referrer-when-downgrade","body":"{\"status_type\":\"subscribe\",\"subscription\":{\"endpoint\":\"https://fcm.googleapis.com/fcm/send/fiGP6rOnDwE:APA91bEYi2Oydq9c5zMug2QeZK3VDd089-7-YylSnUFqpqc3WlFrZT2tkejzROplb0SHCMA2Tc9Xq0ujhpUa3S-8yWpONgNJ4tFEkyy0m3jcvi3BP2FtNp6_pbdgtLP2nwbuDjkHpc8r\",\"expirationTime\":null,\"keys\":{\"p256dh\":\"BGSJ9xrSKmkTKXjMGYfFAWZ2c_Cj44QJ1k5HkvsDPF-8iphCdQAaSS_ob3G7MOSzsEO-L7fUVs7US21MfuBV-4c\",\"auth\":\"v8RL7EgDnBlSXnvT7PhvVw\"}},\"browser\":\"chrome\"}","method":"POST","mode":"cors"});

这是this tutorial的客户端实现部分。

任何东西看起来都不正确吗?

1 个答案:

答案 0 :(得分:1)

仅将4XX用于客户端错误,将5XX用于服务器错误是一种惯例。两种类型的错误均源自服务器。您应该查看服务器代码以确定错误原因是400还是500。

没有正在使用的服务器代码,很难给出确切的原因。由于我无权访问您的服务器,因此这里是a similar server file,因此您可以了解如何生成错误:

  • return HttpResponse(status=400)表示如果尝试从请求正文中解析JSON时存在Python ValueError,则服务器将返回400错误代码。
  • 此示例没有类似return HttpResponse(status=500)的代码,但这将导致500状态代码。

我猜由于this line in urls.py,像save_info这样的方法负责处理对http://example.com/webpush/save_information/...的请求:

url(r'^save_information', views.save_info, name='save_webpush_info')

因此,您可以在服务器代码中查找类似的内容(也可以将它们添加到您的问题中)。

在您链接的教程的~/djangopush/djangopush/views.py中有一个服务器发送错误的示例,但我认为它与您获取的API没有直接关系。但是,它发送状态代码的方式略有不同:

  • return JsonResponse(status=400, data={"message": "Invalid data format"})
  • return JsonResponse(status=500, data={"message": "An error occurred"})

同样,以上示例几乎肯定不是导致错误的直接原因。它们只是寻找内容的指南。如果您共享适当的服务器文件,我们可以为您提供更具体的原因。