κ²μ ν λ μμμ μΌμ’
μ μ΅μ νκ°μλ κ² κ°μ΅λλ€. μ΄κ²μ λμκ² μ’μ§ μμΌλ©° μμμμ μμ±μ μ κ±°ν©λλ€.
λΉμ΄ μμ μ μλ λ°°μ΄μ 보μ νλ μμ±μ΄ μλ κ°μ²΄λ₯Ό 보λ΄λ €κ³ ν©λλ€.
λ°°μ΄μ΄ λΉμ΄ μμ λλ§λ€ 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
λ°λΌμ 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="
κ°μ₯ μ μ©ν λκΈ
μ΄ νλ‘μ νΈμ μ°Έμ¬νλ μ¬λλ€ μ€ λꡬλ μ΄μ λν μ견μ κ°μ§κ³ μμ§ μλ€λ κ²μ΄ λ―ΏκΈ°μ§ μμ΅λλ€. μμ λ΅λ³μ μΈκΈλ μ¬μ© μ¬λ‘λ₯Ό μ΄ν΄νμ§ λͺ»νλ―λ‘ μ ν¨νμ§ μμ΅λλ€. λΆλͺ ν μ§λ¬Έμ΄ "application/x-www-form-urlencoded"μΈλ° "application/json"μ λν μνμ μ 곡νλ μ΄μ λ 무μμ λκΉ? μ΄κ²μ μ¬μ ν ββλ°μνλ λ¬Έμ μ΄μ§λ§ μ¬νκ²λμ£Όμλ₯Ό κΈ°μΈμ΄μ§ μμ΅λλ€.
λ΄ ν μ€νΈμμ μ μλμ§ μμ λΉ λ°°μ΄μ κ²°κ³Όμμ μ¬λΌμ§κ³ 'null'μ url-encoded-stringμ λνλ©λλ€.
json:
{ results: { arr1: [], arr2: undefined, arr3: null, foo: { arr4: [] } } }
λ§ ν¬ν¨νλ URLμ μμ±ν©λλ€.
"?results%5Barr3%5D="