teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

codeを追記

2017/03/13 11:36

投稿

yosuke_furukawa
yosuke_furukawa

スコア390

answer CHANGED
@@ -16,3 +16,41 @@
16
16
  headers : {"Content-Type" : "application/json", "Content-Length": body.length }
17
17
  };
18
18
  ```
19
+
20
+ 追記:
21
+
22
+ 以下の方法で Content-Length さえ渡せば body を渡せることが確認できました。
23
+
24
+
25
+ ```javascript
26
+ const http = require('http');
27
+
28
+ const body = 'hello server';
29
+ let options = {
30
+ host : 'localhost',
31
+ path : '/delete',
32
+ method : 'DELETE',
33
+ headers : {"Content-Type" : "application/json", "Content-Length": body.length}
34
+ };
35
+
36
+ const server = http.createServer((req, res) => {
37
+ let chunk = '';
38
+ req.on('data', (d) => chunk += d);
39
+ req.on('end', () => {
40
+ res.end('hello client ' + chunk);
41
+ });
42
+ });
43
+
44
+ server.listen(0, () => {
45
+ const port = server.address().port;
46
+ options.port = port;
47
+ const req = http.request(options, (res) => {
48
+ // hello client hello server
49
+ res.pipe(process.stdout);
50
+ });
51
+
52
+ req.write(body)
53
+ req.end();
54
+ })
55
+
56
+ ```

1

誤字修正 

2017/03/13 11:36

投稿

yosuke_furukawa
yosuke_furukawa

スコア390

answer CHANGED
@@ -3,7 +3,7 @@
3
3
  この仕様上、 content-length がリクエストパラメータに付与されていないことになると、多くのサーバでは GET/DELETE の際にどこまでbodyとして読んだら良いのかわからないことがあります。
4
4
 
5
5
  このため、多くのweb サーバでは GET/DELETE でbodyを付けることに対応していないことが多いです。
6
- そして node.js の場合は、 http-parser で DELETE に body がない場合は無視します。
6
+ そして node.js の場合は、 http-parser で DELETE に body があろうがかろうが、Content-Lengthがない場合は無視します。
7
7
 
8
8
  ちゃんと試してはいないですが、DELETE時に content-length パラメータをheaderに付与すればもしかしたら読んでくれる可能性があります。
9
9
 
@@ -15,4 +15,4 @@
15
15
  method : 'DELETE',
16
16
  headers : {"Content-Type" : "application/json", "Content-Length": body.length }
17
17
  };
18
- ```
18
+ ```