请求命令等效于cURL命令不起作用

时间:2017-08-18 07:35:26

标签: node.js curl request

cURL命令:

curl -F "json_query=</Users/somename/Desktop/API/payload/upload_image_payload.json" -F "file=@/Users/somename/Desktop/API/payload/image.png" http://url/api/upload_outgoing_media.php

node.js命令:

var options = { method: 'POST',
                url: 'http://url/api/upload_outgoing_media.php',
                headers: 
                { 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' },
                formData: 
                { 
                    json_query: 
                        { value: 'fs.createReadStream("/Users/somename/Desktop/API/payload/upload_image_payload.json")',
                            options:
                            { filename: '/Users/somename/Desktop/API/payload/upload_image_payload.json',
                                contentType: null } }
                        },
                    file: 
                        { value: 'fs.createReadStream("/Users/somename/Desktop/API/payload/image.png")',
                            options:
                            { filename: '/Users/somename/Desktop/API/payload/image.png',
                                contentType: null } },

                };
                console.log("second")
                request(options, function (error, response, body) {
                if (error) throw new Error(error);

                console.log(response.body);
                });

预期产出: 的 {&#34;元&#34;:空,&#34;有效载荷&#34; {&#34;文件名&#34;:&#34; image.png&#34;}&#34;错误&#34;:假}

退回输出: 的 {&#34;元&#34;:空,&#34;有效载荷&#34;:空,&#34;错误&#34; {&#34; ERRORTEXT&#34;:&#34;无数据&#34;&#34;错误代码&#34;:空}}

1 个答案:

答案 0 :(得分:0)

找到解决方案。

  

&#34;&LT;&#34;

curl查询中的

是加载json文件的内容而不是json文件本身,因此在NODE QUERY中它必须是: -

json_query:jsonData - &gt;你的json文件数据

formData: 
        { 
         json_query: jsonData,
          file:
          { value: 'fs.createReadStream("/Users/somename/Desktop/API/payload/image.png")',
           options:
           { filename: '/Users/somename/Desktop/API/payload/image.png',
            contentType: null } }

        }};
  

这有效并且正在返回预期的输出。