发送“原始"负载到 Axios [英] Send "raw" payload to Axios

查看:12
本文介绍了发送“原始"负载到 Axios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何向 Axios 发送原始负载/请求正文?

How can I send a raw payload/request body to Axios?

我试图调用的端点期望请求正文只是一个字符串,它将被抓取并使用.

The endpoint I'm trying to call expects the request body to just be a string which it'll scoop up and use.

如果我尝试为 requestBody 仅将字符串传递给 axios.post(),它会将其转换为没有值的对象 ({ "this+is+my+message": "" }) 并最终像这样解析 "this+is+my+message=".

If I try to just pass a string to axios.post() for the requestBody, it'll convert it to an object with no value ({ "this+is+my+message": "" }) and ends up getting parsed like this "this+is+my+message=".

我查看了文档,但找不到任何似乎有效的选项.transformRequest 似乎是最明显的,但它在字符串中发送,我发送了字符串(字面意思是 d => d),但它似乎仍然转换它到一个无价值的 JSON 对象.

I checked the documentation, but couldn't find any option that seemed to work. transformRequest seemed to be the most obvious, but it sent in the string and I sent out the string (literally d => d), but it still seemed to convert it to a valueless JSON object.

推荐答案

事实证明,如果我将 Content-Type 标头设置为 text/plain,它就赢了不将其转换为 JSON 或表单数据,而是根据需要发送.

It turns out, if I set the Content-Type header to text/plain, it won't convert it to JSON or form data and will send it as I want.

axios.post('/my-url', 'my message text', {
  headers: { 'Content-Type': 'text/plain' }
});

这篇关于发送“原始"负载到 Axios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆