首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Formdata请求ShortPixel接口

Formdata请求ShortPixel接口
EN

Stack Overflow用户
提问于 2018-11-26 08:06:07
回答 1查看 59关注 0票数 0

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

代码语言:javascript
复制
{ Status: { Code: -115, Message: 'Uploaded files are missing.' } } }

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

代码语言:javascript
复制
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)
  });
EN

回答 1

Stack Overflow用户

发布于 2019-01-16 19:08:04

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

代码语言:javascript
复制
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对象的任何变量。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53473262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档