Flask Api working Half the time- works flawlessly when sending post with python but not with javascript

huangapple go评论79阅读模式
英文:

Flask Api working Half the time- works flawlessly when sending post with python but not with javascript

问题

以下是翻译好的部分:

Hi I created an api that scrapes a website and returns values as a JSON file using python flask
我创建了一个 API,使用 Python Flask 爬取一个网站并以 JSON 文件的形式返回数值。

For simple testing I set up a web Interface where the JSON is returned using that same API and it works flawlessly, but when I use the Javascript Fetch function in my react app to call it I keep getting different erros on the server side.
为了进行简单的测试,我设置了一个 Web 界面,在该界面上使用相同的 API 返回 JSON 数据,它运行得非常顺利。但是,当我在我的 React 应用中使用 JavaScript 的 Fetch 函数来调用它时,我一直在服务器端收到不同的错误。

This is they python code that workds perfectly
这是 Python 代码,它完美运行。

and here is the javascript code that is not working properly
这是 JavaScript 代码,它没有正常工作。

They are sending the exact Same request but getting different reponses
它们发送了完全相同的请求,但收到了不同的响应。

For refrence Here is the github repo with my API
供参考,这是我的 API 的 GitHub 仓库链接。
https://github.com/amaheshwari01/POWERSCRAPER

EDIT: My Javascript code is written with react
编辑:我的 JavaScript 代码是使用 React 编写的。

英文:

Hi I created an api that scrapes a website and returns values as a JSON file using python flask
For simple testing I set up a web Interface where the JSON is returned using that same API and it works flawlessly, but when I use the Javascript Fetch function in my react app to call it I keep getting different erros on the server side.

import requests
import json
data = {
    'act': '',
    'pw': '',
    'req': 'grades',
}

response = requests.post('http://127.0.0.1:5000/', data=data)
print(response.text)

This is they python code that workds perfectly

and here is the javascript code that is not working properly

  useEffect(() => {

    fetch('http://127.0.0.1:5000/', {
    method: 'POST',
    
    body: new URLSearchParams({
        'act': '',
    'pw': '',
    'req': 'grades'
    })
    }).then(response => {
      console.log('Response:', response)
      return response.json();
}).then(response => console.log(response))
  }, []); 

They are sending the exact Same request but getting different reponses

What it should look like(what I get when running the python code

What happens when I run the Javascript code

For refrence Here is the github repo with my API
https://github.com/amaheshwari01/POWERSCRAPER

EDIT: My Javascript code is written with react

答案1

得分: 0

看着你的API,你需要一个表单提交。尝试更新你的JavaScript代码,使用FormData对象,看看是否可以工作。

const formData = new FormData();

formData.append('act', '');
formData.append('pw', '');
formData.append('req', 'grades');

fetch('http://127.0.0.1:5000/', {
    method: 'POST',
    body: formData
})
.then(response => {
    console.log('Response:', response)
    return response.json();
}).then(response => console.log(response))
英文:

Looking at your API you are expecting a Form post. Try updating your JavaScript to use the FormData object and see if that works.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const formData = new FormData();

formData.append(&#39;act&#39;, &#39;&#39;);
formData.append(&#39;pw&#39;, &#39;&#39;);
formData.append(&#39;req&#39;, &#39;grades&#39;);

fetch(&#39;http://127.0.0.1:5000/&#39;, {
    method: &#39;POST&#39;,
    body: formData
})
.then(response =&gt; {
      console.log(&#39;Response:&#39;, response)
      return response.json();
}).then(response =&gt; console.log(response))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 05:40:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定