Request: 양식에 빈 배열이 μžˆλŠ” κ²Œμ‹œλ¬Όμ€ 속성을 μ œκ±°ν•©λ‹ˆλ‹€.

에 λ§Œλ“  2017λ…„ 08μ›” 15일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: request/request

μš”μ•½


κ²Œμ‹œ ν•  λ•Œ 양식에 μΌμ’…μ˜ μ΅œμ ν™”κ°€μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. 이것은 λ‚˜μ—κ²Œ 쒋지 μ•ŠμœΌλ©° μ–‘μ‹μ—μ„œ 속성을 μ œκ±°ν•©λ‹ˆλ‹€.
λΉ„μ–΄ μžˆμ„ 수 μžˆλŠ” 배열을 λ³΄μœ ν•˜λŠ” 속성이 μžˆλŠ” 개체λ₯Ό 보내렀고 ν•©λ‹ˆλ‹€.
배열이 λΉ„μ–΄ μžˆμ„ λ•Œλ§ˆλ‹€ MissingPropertyErrorκ°€ λ°˜ν™˜λ˜λ©° μ΄λŠ” 빈 배열이 μ‹€μ œλ‘œ 속성을 갖지 μ•ŠλŠ” 것보닀 값을 λ³΄μœ ν•˜κΈ° λ•Œλ¬Έμ— 잘λͺ»λœ κ²ƒμž…λ‹ˆλ‹€.

μž¬ν˜„ν•˜κΈ° μœ„ν•œ κ°€μž₯ κ°„λ‹¨ν•œ 예

const request = require( 'request' )
const bodyParser = require('body-parser')
const app = require('express')()

app.use( bodyParser.json() )
app.use( bodyParser.urlencoded( { extended: true } ) )

app.post('/', function( req, res ){
    console.log( 'received:', req.body )
    res.send('ok')
})
app.listen( 53153 )

const req = {
    url: 'http://localhost:53153',
    method: 'POST',
    headers: {
        'Content-Type' : 'application/x-www-form-urlencoded'
    },
    form: {
        emptyArray: [],
        filledArray: [{something:[]}]
    }
}
console.log( 'sending:', req )
request( req , function( err, httpResponse, body ){
    process.exit(0)
})

μ˜ˆμƒλ˜λŠ” λ™μž‘




μœ νš¨ν•œ jsonμ΄λ―€λ‘œ send κ°œμ²΄κ°€ μˆ˜μ‹ λ  κ²ƒμœΌλ‘œ μ˜ˆμƒν–ˆμŠ΅λ‹ˆλ‹€.

$ node index.js
sending: { url: 'http://localhost:53153',
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  form: { emptyArray: [], filledArray: [ { something: [] } ] }
received: { emptyArray: [], filledArray: [ { something: [] } ] }

ν˜„μž¬ 행동



와이어샀크λ₯Ό 보면 μˆ˜μ‹ λœ κ°μ²΄λŠ” 빈 객체일 뿐이며 μ „μ†‘λ˜λŠ” μ½˜ν…μΈ  κΈΈμ΄λŠ” 0μž…λ‹ˆλ‹€.

$ node index.js
sending: { url: 'http://localhost:53153',
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  form: { emptyArray: [], filledArray: [ { something: [] } ] }
received: {}

배열에 null을 μΆ”κ°€ν•˜λ©΄ λ™μž‘μ΄ λ³€κ²½λ©λ‹ˆλ‹€.

$ node index.js
sending: { url: 'http://localhost:53153',
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  form: { emptyArray: [ null ], filledArray: [ { something: [ null ] } ] }
received: { emptyArray: [ '' ], filledArray: [ { something: [ '' ] } ] }

κ·ΈλŸ¬λ‚˜ 배열에 undefinedλ₯Ό 좔가해도 λ™μž‘μ€ λ³€κ²½λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

$ node index.js
sending: { url: 'http://localhost:53153',
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  form: { emptyArray: [ undefined ], filledArray: [ { something: [ undefined ] } ] }
received: {}

κ°€λŠ₯ν•œ ν•΄κ²°μ±…


λ¬Έλ§₯


λ‹Ήμ‹ μ˜ ν™˜κ²½

| μ†Œν”„νŠΈμ›¨μ–΄ | 버전
| ---------------- | -------
| μš”μ²­ | 2.81.0
| λ…Έλ“œ | 8.3.0
| npm | 5.1.0
| 운영 체제 | μš°λΆ„νˆ¬ 16.04.2 LTS

stale

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

이 ν”„λ‘œμ νŠΈμ— μ°Έμ—¬ν•˜λŠ” μ‚¬λžŒλ“€ 쀑 λˆ„κ΅¬λ„ 이에 λŒ€ν•œ μ˜κ²¬μ„ 가지고 μžˆμ§€ μ•Šλ‹€λŠ” 것이 믿기지 μ•ŠμŠ΅λ‹ˆλ‹€. μœ„μ˜ 닡변은 μ–ΈκΈ‰λœ μ‚¬μš© 사둀λ₯Ό μ΄ν•΄ν•˜μ§€ λͺ»ν•˜λ―€λ‘œ μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λΆ„λͺ…νžˆ 질문이 "application/x-www-form-urlencoded"인데 "application/json"에 λŒ€ν•œ μƒ˜ν”Œμ„ μ œκ³΅ν•˜λŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ? 이것은 μ—¬μ „νžˆ β€‹β€‹λ°œμƒν•˜λŠ” λ¬Έμ œμ΄μ§€λ§Œ μŠ¬ν”„κ²Œλ„μ£Όμ˜λ₯Ό κΈ°μšΈμ΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ‚΄ ν…ŒμŠ€νŠΈμ—μ„œ μ •μ˜λ˜μ§€ μ•Šμ€ 빈 배열은 κ²°κ³Όμ—μ„œ 사라지고 'null'은 url-encoded-string에 λ‚˜νƒ€λ‚©λ‹ˆλ‹€.

json:
{ results: { arr1: [], arr2: undefined, arr3: null, foo: { arr4: [] } } }
만 ν¬ν•¨ν•˜λŠ” URL을 μƒμ„±ν•©λ‹ˆλ‹€.
"?results%5Barr3%5D="

λͺ¨λ“  3 λŒ“κΈ€

λ”°λΌμ„œ JSON 객체가 λ¬Έμžμ—΄λ‘œ λ³€ν™˜λ˜κ³  μ „μ†‘λ˜κΈ° 전에 μš”μ²­μ˜ 본문으둜 μ„€μ •λ˜λŠ” κ²ƒμœΌλ‘œ λ³΄μž…λ‹ˆλ‹€. 이것은 'form' 속성을 지정할 λ•Œμ˜ κΈ°λ³Έ λ™μž‘μž…λ‹ˆλ‹€. 이 λΌμ΄λΈŒλŸ¬λ¦¬κ°€ JSON을 λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜λŠ” 데 μ‚¬μš©ν•˜λŠ” 방법은 빈 배열을 μ œκ±°ν•©λ‹ˆλ‹€. ν•΄μ•Ό ν•  일은 'form' 속성을 μ „λ‹¬ν•˜λŠ” λŒ€μ‹  'body' 속성을 μ „λ‹¬ν•˜κ³  JSON 객체λ₯Ό JSON.stringifyν•˜μ—¬ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄:

μ°Έκ³ : 헀더λ₯Ό μ—‰λ§μœΌλ‘œ λ§Œλ“€κ³  μ½˜ν…μΈ  μœ ν˜•μœΌλ‘œ application/json을 μ‚¬μš©ν•΄μ•Ό ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” νš¨κ³Όκ°€ μžˆλŠ”μ§€ μ‹€ν—˜ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

{
    url: 'http://localhost:53153',
    method: 'POST',
    headers: { 'Content-Type': 'application/json'},
    body: JSON.stringify({ emptyArray: [], filledArray: [ { something: [] } ] })
}

이 λ¬Έμ œλŠ” 졜근 ν™œλ™μ΄ μ—†μ—ˆκΈ° λ•Œλ¬Έμ— μžλ™μœΌλ‘œ 였래된 κ²ƒμœΌλ‘œ ν‘œμ‹œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. 더 이상 ν™œλ™μ΄ μ—†μœΌλ©΄ νμ‡„λ©λ‹ˆλ‹€. κ·€ν•˜μ˜ 기여에 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€.

이 ν”„λ‘œμ νŠΈμ— μ°Έμ—¬ν•˜λŠ” μ‚¬λžŒλ“€ 쀑 λˆ„κ΅¬λ„ 이에 λŒ€ν•œ μ˜κ²¬μ„ 가지고 μžˆμ§€ μ•Šλ‹€λŠ” 것이 믿기지 μ•ŠμŠ΅λ‹ˆλ‹€. μœ„μ˜ 닡변은 μ–ΈκΈ‰λœ μ‚¬μš© 사둀λ₯Ό μ΄ν•΄ν•˜μ§€ λͺ»ν•˜λ―€λ‘œ μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λΆ„λͺ…νžˆ 질문이 "application/x-www-form-urlencoded"인데 "application/json"에 λŒ€ν•œ μƒ˜ν”Œμ„ μ œκ³΅ν•˜λŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ? 이것은 μ—¬μ „νžˆ β€‹β€‹λ°œμƒν•˜λŠ” λ¬Έμ œμ΄μ§€λ§Œ μŠ¬ν”„κ²Œλ„μ£Όμ˜λ₯Ό κΈ°μšΈμ΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ‚΄ ν…ŒμŠ€νŠΈμ—μ„œ μ •μ˜λ˜μ§€ μ•Šμ€ 빈 배열은 κ²°κ³Όμ—μ„œ 사라지고 'null'은 url-encoded-string에 λ‚˜νƒ€λ‚©λ‹ˆλ‹€.

json:
{ results: { arr1: [], arr2: undefined, arr3: null, foo: { arr4: [] } } }
만 ν¬ν•¨ν•˜λŠ” URL을 μƒμ„±ν•©λ‹ˆλ‹€.
"?results%5Barr3%5D="

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰