nodejs request module里的json参数的一个坑

简介: 今天工作的时候遇到一个坑,在客户端用nodejs给服务器发送HTTP请求,服务器老是报错:In the context of Data Services an unknown internal server error occurred经过服务器端调试发现,服务器根本就没有正确解析出这个请求的content-type。

今天工作的时候遇到一个坑,在客户端用nodejs给服务器发送HTTP请求,服务器老是报错:In the context of Data Services an unknown internal server error occurred

经过服务器端调试发现,服务器根本就没有正确解析出这个请求的content-type。在postman里能工作的场景下,正确解析出的content-type是multipart/mixed:

而我的nodejs代码里明明指定了这个content-type的啊?

经过一行行代码分析,最后发现问题出在第63行的json字段的值。我错误的赋成了true。

这个参数起什么作用?调试一下就知道了。如果为true,进入第403行。

如果请求内部有entity的content-type不是application/x-www-form-urlencoded, 则进入第1293行。

safeStringify的实现逻辑就是浏览器原生的JSON.stringify, 把应用程序传入的json对象序列化成字符串。但是我的代码里,传入request module的请求体是一个字符串,而json参数设的又是true,所以逻辑上就不对了。把这个json参数的值改为false后,一切正常。

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关文章
|
3月前
|
Kubernetes 调度 容器
K8S 性能优化 -K8S Node 参数调优
K8S 性能优化 -K8S Node 参数调优
|
4月前
|
JavaScript
node下的two.js调用one.js出现无法编译问题 Cannot find module ‘c:
node下的two.js调用one.js出现无法编译问题 Cannot find module ‘c:
48 0
|
5月前
|
JSON JavaScript API
Node.js(nodejs)对本地JSON文件进行增、删、改、查操作(轻车熟路)
Node.js(nodejs)对本地JSON文件进行增、删、改、查操作(轻车熟路)
|
4月前
|
Web App开发 前端开发
Chrome 浏览器插件 V3 版本 Manifest.json 文件中 Action 的类型(Types)、方法(Methods)和事件(Events)的属性和参数解析
Chrome 浏览器插件 V3 版本 Manifest.json 文件中 Action 的类型(Types)、方法(Methods)和事件(Events)的属性和参数解析
161 0
|
6月前
|
JavaScript
Syntax Error: Error: Cannot find module ‘node-sass‘
Syntax Error: Error: Cannot find module ‘node-sass‘
67 0
|
6月前
|
小程序
小程序wepy踩坑-Cannot find module 'D:\node_modules\npm\bin\npm-cli.js'
小程序wepy踩坑-Cannot find module 'D:\node_modules\npm\bin\npm-cli.js'
106 0
|
26天前
报错/ ./node_modules/axios/lib/platform/index.js Module parse failed: Unexpected token (5:2)怎么解决?
报错/ ./node_modules/axios/lib/platform/index.js Module parse failed: Unexpected token (5:2)怎么解决?
|
4天前
|
资源调度 JavaScript 内存技术
error @achrinza/node-ipc@9.2.2: The engine “node“ is incompatible with this module. Expected version
error @achrinza/node-ipc@9.2.2: The engine “node“ is incompatible with this module. Expected version
|
17天前
|
JSON API 数据格式
python的request库如何拿到json的返回值
python的request库如何拿到json的返回值
10 0
|
2月前
Module build failed (from ./node_modules/eslint-loader/index.js)
Module build failed (from ./node_modules/eslint-loader/index.js)
64 0
Module build failed (from ./node_modules/eslint-loader/index.js)