2017-11-03 11 views
1

を要求:NodeJSにカール要求を変換し、私は現在、このようにカールして要求の例を示しますAPIに取り組んでいます

curl -v -H "Accept-Token: mysecret" -H "User-Token: my_user" \ 
    -F "[email protected]_photo.jpg" \ 
    -F "face_detection={\"fast\" : true}" \ 
    -F "age_detection={\"something\" : true}" \ 
    127.0.0.1:8080/vision_batch 

私はそのような要求を行うと、これはnetcatをを印刷しているものです。

POST /vision_batch HTTP/1.1 
Host: 127.0.0.1:9000 
User-Agent: curl/7.47.0 
Accept: */* 
Accept-Token: test_token 
User-Token: test 
Content-Length: 635202 
Expect: 100-continue 
Content-Type: multipart/form-data; boundary=------------------------a32bed4123bace7d 

--------------------------a32bed4123bace7d 
Content-Disposition: form-data; name="filename"; filename="photo.png" 
Content-Type: application/octet-stream 
<binary_content> 
--------------------------a32bed4123bace7d 
Content-Disposition: form-data; name="face_detection" 

{} 
--------------------------a32bed4123bace7d 
Content-Disposition: form-data; name="qr_recognition" 

{} 
--------------------------a32bed4123bace7d-- 

しかし、この複数のフォームをNodeJSで翻訳する方法はありません。

function getOptions(buffer, service) { 
    return { 
    url: 'http://127.0.0.1:9001/' + service, 
    headers: headers, 
    method: 'POST', 
    formData: { 
     filename: buffer, 
     face_recognition: [], 
     age_detection: [] 
    } 
    } 
} 

var res_json = {}; 
request(getOptions(buffer, 'face_recognition'), function(error, response, body) { 
}); 

問題は、APIが私をno args返しているということである。ここでは

が私の現在のコードです...私は現在、各フォームに対する複数の要求をしていますが、私は、画像を毎回送信する必要があります。そして効果的に、netcatをは次のように印刷されています

POST /vision_batch HTTP/1.1 
Accept-Token: YE6geenfzrFiT88O 
User-Token: ericsson_event 
host: 127.0.0.1:9000 
content-type: multipart/form-data; boundary=--------------------------648089449032824937983411 
content-length: 663146 
Connection: close 

----------------------------648089449032824937983411 
Content-Disposition: form-data; name="filename" 
Content-Type: application/octet-stream 
<binary_content> 
----------------------------648089449032824937983411-- 

問題は、私は、要求のフィールドFORMDATAを変更しないでくださいどのようにということです...

答えて

1

あなたformDataは、ほぼ正しいです。

しかし、requestは(マルチパート)フォーム処理にform-dataを使用し、form-dataは、文字列、ストリームとの値のBuffer Sのみをサポートしているので、あなたは(usageこのissueを参照)JSONにあなたのオブジェクトを文字列化する必要があります。これはまさに(headersと前人口bufferを想定して)あなたのcurl要求を複製します

function getOptions(buffer, service) { 
    return { 
    url: 'http://127.0.0.1:9001/' + service, 
    headers: headers, 
    method: 'POST', 
    formData: { 
     filename: buffer, 
     face_recognition: JSON.stringify({fast: true}), 
     age_detection: JSON.stringify({something: true}) 
    } 
    } 
} 
request(getOptions(buffer, 'face_recognition')); 
関連する問題