这个curl命令的node js等效项是什么?

问题描述 投票:0回答:1

我有这个curl命令,我需要在node js中编写:

curl -X POST \
  https://image.adobe.io/pie/psdService/renditionCreate \
  -H "Authorization: Bearer $token"  \
  -H "x-api-key: $apiKey" \
  -H "Content-Type: application/json" \
  -d '{
  "inputs":[
    {
      "href":"<SIGNED_GET_URL>",
      "storage":"<storage>"
    }
  ],
  "outputs":[
    {
      "href":"<SIGNED_POST_URL1>",          
      "width": 512,
      "storage":"<storage>",
      "type":"image/jpeg"      
    },
    {
      "href":"<SIGNED_POST_URL2>",
      "storage":"<storage>",
      "type":"image/png"
    }
  ]
}'

我不明白怎么写,特别是输入和输出部分。

到目前为止我所拥有的:

var url = "https://image.adobe.io/pie/psdService/renditionCreate"
var options = {method: "post"};
options.headers = { 
    Authorization: `Bearer $token`,
    x-api-key: `$apiKey`,
    Content-Type: application/json
}
options.body = {
  "inputs":[
    {
      "href":"<SIGNED_GET_URL>",
      "storage":"<storage>"
    }
  ],
  "outputs":[
    {
      "href":"<SIGNED_POST_URL1>",          
      "width": 512,
      "storage":"<storage>",
      "type":"image/jpeg"      
    },
    {
      "href":"<SIGNED_POST_URL2>",
      "storage":"<storage>",
      "type":"image/png"
    }
  ]
}

var results = await fetch(url, options);

代码示例来自这里

更新:我接到工作电话,稍后会发布。

javascript node.js curl fetch-api
1个回答
0
投票

这是转换后的curl 调用。由于某种原因,标头标记变量没有被替换,所以我之后直接重新定义了它们。

我已内联添加回复。

  async createRenditionOfPSDCall(token, apiKey, inputs, outputs) {
    var url = "https://image.adobe.io/pie/psdService/renditionCreate";

    var options: any = new Object();

    options.headers = {
      "Authorization": `Bearer $token`,
      "x-api-key": `$apiKey`,
      "Content-Type": "application/json"
    }

    options.headers["Authorization"] = "Bearer " + token;
    options.headers["x-api-key"] = apiKey;
    options.headers["Content-Type"] = "application/json";

    // make array
    if (("length" in inputs)==false) {
      inputs = [inputs];
    }

    if (("length" in outputs)==false) {
      outputs = [outputs];
    }

    options.body = JSON.stringify({
      "inputs": inputs,
      "outputs": outputs
    })

    options.method = "post";
    
    var results = await fetch(url, options);
    var text = await results.text();

    if (text.indexOf("{")==0) {
      var links = JSON.parse(text);
      var href = links.self.href;
    }

    var x = {
      "_links": {
        "self": {
          "href": "https://image.adobe.io/pie/psdService/status/85cedc52-da8e-4b36-a0bb-8e4fdb4fd7b2"
        }
      }
    }

    return results;
  }
© www.soinside.com 2019 - 2024. All rights reserved.