简体   繁体   English

将 Python API 调用转换为 NodeJS Axios Z8A5DA52ED126447D359E70C0572A 如何正确调用它?

[英]Translating Python API call to NodeJS Axios api call - how do I format it correctly?

I'm getting a 400 error and isAxiosError: true .我收到 400 错误和isAxiosError: true I think the problem is the auth line is not formatted correctly, or I'm not quite understanding how params work in axios and what's needed by the api?我认为问题在于 auth 行的格式不正确,或者我不太了解参数在 axios 中的工作原理以及 api 需要什么? What am I doing wrong in my translating of python to Axios/JS?我在将 python 翻译成 Axios/JS 时做错了什么? Here's the Voila Norbert API documentation .这是Voila Norbert API 文档

Here's my Axios api call.这是我的 Axios api 电话。

axios.post('https://api.voilanorbert.com/2018-01-08/search/name', {
  params: {
    auth: {any_string: API_KEY},
    data: { 
      domain: 'amazon.com',
      name: 'Jeff Bezos'
    }
  }
})

Here's the python version:这是 python 版本:

API_TOKEN = 'abcde'

req = requests.post(
  'https://api.voilanorbert.com/2018-01-08/search/name',
  auth=('any_string', API_TOKEN),
  data = {
      'name': 'Cyril Nicodeme',
      'domain': 'reflectiv.net'
  }
)

According to their documentation, https://github.com/axios/axios , you need to give auth as a separate field, not inside params :根据他们的文档https://github.com/axios/axios ,您需要将auth作为单独的字段提供,而不是在params内:

axios.post('https://api.voilanorbert.com/2018-01-08/search/name', {
  auth: {
    username: 'any_string',
    password: API_KEY
  },
  data: { 
    domain: 'amazon.com',
    name: 'Jeff Bezos'
  }
})

Updated: removed the nesting of data in params .更新:删除了paramsdata的嵌套。 They should be sent as POST body, not URL params.它们应该作为 POST 正文发送,而不是 URL 参数。

I am a year late with this answer but I found your question while dealing with this same error with the same API.我的这个答案迟到了一年,但我在用相同的 API 处理同样的错误时发现了你的问题。 The API documentation's suggested Python code works for me with a successful response, but I want to do it in Node and the JS code returns a 400 error. API 文档建议的 Python 代码对我有用,响应成功,但我想在 Node 中执行此操作,JS 代码返回 400 错误。 I'm sharing my solution in case it helps others in the future.我正在分享我的解决方案,以防将来对其他人有所帮助。

I believe the core issue is that the API expects the data to be posted as form data, not as JSON.我认为核心问题是 API 期望数据以表单数据的形式发布,而不是 JSON。 I followed an example in another post to post form data with Axios but still was receiving a 400 error.我按照另一篇文章中的示例使用 Axios 发布表单数据,但仍然收到 400 错误。

Then I tried posting it using the request package instead of Axios, and that results in a successful response (no error):然后我尝试使用请求 package 而不是 Axios 发布它,这会导致成功响应(无错误):

const request = require('request');

var data = {'name': 'Jeff Bezos', 'domain': 'amazon.com'};

request.post({
    url: 'https://any_string:API_KEY@api.voilanorbert.com/2018-01-08/search/name',
    form: data,
}, function(error, response, body){
    console.log(body);
});

This example works when the data is included in the form: field but does not work when it is in body:此示例在数据包含在表单中时有效:字段但在正文中时无效:

Please note the request package is deprecated and in maintenance mode.请注意请求 package 已弃用并处于维护模式。

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

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