简体   繁体   English

如何添加嵌套对象以获取 POST 正文?

[英]How to add nested object to fetch POST body?

Given a simple fetch request ie给定一个简单的获取请求,即

   fetch('https://api.com/endpoint', {
      method: "POST"
    });

Is it possible / how can following object be converted to query params / query string to append to the fetch request?是否有可能/如何将以下对象转换为查询参数/查询字符串以附加到获取请求? Most methods online recommend using new URLSearchParams helper, however it doesn't seem to work with nested objects and when converted to string these nested objects are returned as [Object object] string.大多数在线方法都推荐使用新的 URLSearchParams助手,但它似乎不适用于嵌套对象,并且当转换为字符串时,这些嵌套对象将作为[Object object]字符串返回。 Ideal solution should not rely on any third party packages.理想的解决方案不应依赖任何第三方软件包。

  {
    side: 'buy',
    symbol: 'AAPL',
    type: 'market',
    qty: '2',
    time_in_force: 'gtc',
    order_class: 'bracket',
    take_profit: {
      limit_price: '200'
    },
    stop_loss: {
      stop_price: '200'
    }
  }

Form your fetch request like this:像这样形成你的获取请求:


 fetch('https://api.com/endpoint', {
      method: "POST",
      headers: {
      'Content-Type': 'application/json'
          },
     body: JSON.stringify(dataYouWannaPost)

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM