Invalid JSON GET Request Express.js

前端 未结 1 556
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 03:43

While writing an API, I have come across a very thorny error: when I try to do a res.send(INSERT JSON) with a Content-Type header application/json

1条回答
  •  长发绾君心
    2021-01-06 03:50

    I believe the problem is that you want to be using Content-Type header in your servers response; not in your request Content-Type header.

    When you use the Content-Type header in your request, Express will read the Content-Type and attempt to interpret the provided information as that Content-Type, in this case, as JSON. Because this is a GET request and thus has no body, Express is trying to interpret an empty string as JSON, which is giving you the error.

    0 讨论(0)
提交回复
热议问题