Axios API调用引发“JSON参数为空错误”

hi3rlvi2  于 2024-01-07  发布在  iOS
关注(0)|答案(1)|浏览(169)

我正在使用Axios在TypeScript中构建一个API调用,API要求在指定JSON-RPC版本和方法的同时以JSON格式进行调用。API提供程序分享了一个 Postman 示例,演示了如何格式化请求正文:

curl --location 'http://localhost:3000/api/get-products' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "call",
    "params": {
        "db_name": "mig_api",
        "user": "[email protected]",
        "password": "Test123!",
        "LanguageCode": "en_EN"
    }
}'

字符串
然而,我的目标是通过Axios调用发送所有必要的数据,并通过直接访问URL来接收响应。我面临的问题是,每次我进行调用时,我总是从服务器收到相同的错误:

{ jsonrpc: '2.0', id: null, result: 'JSON param empty error' }


下面是API调用的代码:

import axios, { AxiosResponse } from "axios";
import { ConstDataSet } from '../../core/constants/data-set.coonstant';

export class DataService {

    private apiUrl: string = 'https://api.provider.com/products/get_json';

    async fetchData(constDataSet: typeof ConstDataSet): Promise<AxiosResponse | void> {

        return await axios.post(this.apiUrl, {
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                jsonrpc: "2.0",
                method: "call",
            },
            params: {
                username: constDataSet.user,
                password: constDataSet.password,
                db_name: constDataSet.db_name,
                LanguageCode: constDataSet.LanguageCode
            }
        }).then(response => {
            console.log(response.data);
        }).catch(error => console.log(error));
    }
}

export default new DataService();


constDataSet从一个已分配环境变量的导出常量中获取值。我知道.env工作正常,因为我正在使用它连接数据库和应用程序PORT,它们工作正常:

import { DataSet } from "../interfaces/data-set.interface";

export const ConstDataSet: DataSet =  {
    db_name: process.env.API_DATABASE,
    user: process.env.API_USER,
    password: process.env.API_PASSWORD,
    LanguageCode: process.env.API_LANGUAGE_CODE
} as const


即使我尝试直接在axios调用中硬编码数据,也会发生同样的错误。我尝试了不同的方法来发送所需的数据,例如:

return await axios.post(this.apiUrl, {
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                jsonrpc: "2.0",
                method: "call",
                username: constDataSet.user,
                password: constDataSet.password,
                db_name: constDataSet.db_name,
                LanguageCode: constDataSet.LanguageCode
            },


还尝试发送params部分中的所有数据,删除头,将头作为数据或参数发送,添加json rpc作为axios.post的parm,如下所示:

const body = {
            jsonrpc: "2.0",
            method: "call",
        }
return await axios.post(this.apiUrl, body {


不幸的是,这些更改都没有解决这个问题,我一直遇到同样的错误。我正在寻求解决和克服这个错误的建议。任何见解或指导都将非常感谢。谢谢!

50few1ms

50few1ms1#

尝试将params作为JSON对象的一部分,因为axios.post接受以下参数:

axios.post(url, data, config)

字符串
交换数据和标头。
试试这个:

return await axios.post(this.apiUrl, {
       jsonrpc: "2.0",
       method: "call",
       params: {
           username: constDataSet.user,
           password: constDataSet.password,
           db_name: constDataSet.db_name,
           LanguageCode: constDataSet.LanguageCode
       }
   }, {
       headers: {
           'Content-Type': 'application/json'
       }
   }).then(response => {
       console.log(response.data);
   }).catch(error => console.log(error));


另外,您可以将DataSet接口更改为如下所示:

interface DataSet {
  jsonrpc: '2.0';
  method: 'call';
  params: {
    username: string;
    password: string;
    db_name: string;
    LanguageCode: string;
  };
}


或者创建组合类型:

interface ConstDataSet {
  params: {
    username: string;
    password: string;
    db_name: string;
    LanguageCode: string;
  };
}

interface JsonRpc {
  jsonrpc: '2.0';
  method: 'call';
}

type Payload = JsonRpc & ConstDataSet;

//...

let payload: Payload = {
  jsonrpc: '2.0',
  method: 'call',
  params: {
    username: constDataSet.user,
    password: constDataSet.password,
    db_name: constDataSet.db_name,
    LanguageCode: constDataSet.LanguageCode
  }
};

相关问题