【问题标题】:Formdata Request ShortPixel APIFormdata 请求 ShortPixel API
【发布时间】:2018-11-26 00:06:06
【问题描述】:

我正在尝试对 ShortPixel 发出请求,以防它是包含多部分/表单数据的请求并且我收到了返回

{ Status: { Code: -115, Message: 'Uploaded files are missing.' } } }

API 链接https://shortpixel.com/api-docs#reducer-api-params

const FormData = require("form-data")
const axios = require('axios')
const data = new FormData();
const url = 'https://api.shortpixel.com/v2/post-reducer.php'

const config = {  headers: {
  'accept': 'application/json',
  'Accept-Language': 'en-US,en;q=0.8',
  'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
}}

data.append('hiroshi.png', fs.createReadStream("/home/hiroshi/Documents/projetos/compress/imagens/original/hiroshi.png"), 'hiroshi.png');


axios.post(url,options,{formData:data}, config)
  .then((response) => {
    console.log(response)
  }).catch((error) => {
   console.log(error)
  });

【问题讨论】:

    标签: node.js axios multipartform-data


    【解决方案1】:

    我不确定请求多部分表单数据是什么意思。我只是将表单数据收集到服务器端的一个对象中,然后像这样发送它:

    let postData =  { 
        "key": "api_key_here",
        "plugin_version": "JS123",
        "lossy": 1,
        "cmyk2rgb": 1,
        "refresh": 1,
        "resize": 3,
        "wait":30,
        "resize_width":100,
        "resize_height":100,
        "urllist":["http://example.com/example.png", "http://example.com/example2.png"]
        }
    
        axios.post('https://api.shortpixel.com/v2/reducer.php',postData, {
            headers: {
            "Content-Type": "application/json"
            }
        })
        .then(function (response) {
            console.log(response.data);
        })
        .catch(function (error) {
            console.log(error);
        });
    

    您可以在我上面创建的postData 对象中添加任何变量。

    edit:哦,我明白了,您使用的是 post-reducer api,而上面我只使用 reducer。你解决了吗?

    【讨论】:

      猜你喜欢
      • 2021-05-11
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 2018-03-22
      • 2021-08-14
      • 1970-01-01
      相关资源
      最近更新 更多