[英]Axios Post request Giving Error 400 , Bad Request
I am using the Hackerrank API for a Project.我正在为一个项目使用 Hackerrank API。 See the official documentation, click here !
看官方文档,点这里!
There is an example on their website which uses UNIREST,他们的网站上有一个使用 UNIREST 的示例,
unirest.post("https://hackerrank-hackerrank.p.rapidapi.com/https://api.hackerrank.com/checker/submission.json")
.header("X-RapidAPI-Host", "hackerrank-hackerrank.p.rapidapi.com")
.header("X-RapidAPI-Key", "a72a0f1b5dmshdc3f55e233876eap1b8939jsnffad2a5b6e6e")
.header("Content-Type", "application/x-www-form-urlencoded")
.send("callback_url=https://mywebsite.com/responseHandler")
.send("source=puts 'Hello World'")
.send("lang=8")
.send("testcases=["This is input 1", "This is input 2"]")
.send("wait=false")
.send("format=json")
.end(function (result) {
console.log(result.status, result.headers, result.body);
});
Since I am using axios, I converted it to an equivalent axios code which looks like:由于我使用的是 axios,我将其转换为等效的 axios 代码,如下所示:
var params = {
"callback_url": "https://mywebsite.com/responseHandler",
"source": "puts 'Hello World'",
"lang": 8,
"testcases": "[\"This is input 1\", \"This is input 2\"]",
"wait": false,
"format": "json"
}
var config = {
mode: "no-cors",
headers: {
"X-RapidAPI-Host": "hackerrank-hackerrank.p.rapidapi.com",
"X-RapidAPI-Key": "a72a0f1b5dmshdc3f55e233876eap1b8939jsnffad2a5b6e6e",
'Access-Control-Allow-Origin': '*',
"Content-Type": "application/x-www-form-urlencoded"
}
}
axios.post("https://hackerrank-hackerrank.p.rapidapi.com/https://api.hackerrank.com/checker/submission.json", params, config)
.catch((error) => {
console.log(error.message);
})
.then((response) => {
console.log(response);
})
I expect this to work just the example shown in the example, but it gives me the following error:我希望这仅适用于示例中显示的示例,但它给了我以下错误:
Request failed with status code 400
请求失败,状态码 400
Error: Request failed with status code 400错误:请求失败,状态码为 400
at createError (createError.js:16)在 createError (createError.js:16)
at settle (settle.js:18)在解决 (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)在 XMLHttpRequest.handleLoad (xhr.js:77)
I am relatively new to this, if someone can point out what i am doing wrong, that would be very helpful!我对此比较陌生,如果有人能指出我做错了什么,那将非常有帮助!
As request Content-Type
is application/x-www-form-urlencoded
, you should pass data as FromData由于请求
Content-Type
是application/x-www-form-urlencoded
,您应该将数据作为FromData传递
var data= new FormData(); // Currently empty
data.append('callback_url', 'https://mywebsite.com/responseHandler');
data.append('source', "puts 'Hello World'");
data.append('lang', '8');
data.append('testcases', "[\"This is input 1\", \"This is input 2\"]");
data.append('wait', false);
data.append('format', "json");
data.append('api_key', "<valid hackerrenk api key>"); // API KEY is mandatory
axios.post("https://hackerrank-hackerrank.p.rapidapi.com/https://api.hackerrank.com/checker/submission.json", data, config)
.catch((error) => {
console.log(error.message);
})
.then((response) => {
console.log(response);
})
Ashish pointed me into the correct direction, but with FormData()
I received a status 415 (Unsupported Media Type) instead of the status 400. Ashish 为我指明了正确的方向,但是使用
FormData()
我收到了状态 415(不支持的媒体类型)而不是状态 400。
What worked for me was using URLSearchParams()
instead of FormData()
, like in this example :对我
URLSearchParams()
是使用URLSearchParams()
而不是FormData()
, 就像在这个例子中一样:
var data= new URLSearchParams(); // Currently empty
data.append('callback_url', 'https://mywebsite.com/responseHandler');
data.append('source', "puts 'Hello World'");
data.append('lang', '8');
data.append('testcases', "[\"This is input 1\", \"This is input 2\"]");
data.append('wait', false);
data.append('format', "json");
data.append('api_key', "<valid hackerrenk api key>"); // API KEY is mandatory
axios.post("https://hackerrank-hackerrank.p.rapidapi.com/https://api.hackerrank.com/checker/submission.json", data, config)
.catch((error) => {
console.log(error.message);
})
.then((response) => {
console.log(response);
})
(I had a different example with a different URL, but can't share my example since it's an URL from my company) (我有一个不同 URL 的不同示例,但无法分享我的示例,因为它是来自我公司的 URL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.