我需要请求其他人为此项目创建的API。在他的文档中,我必须执行以下操作:
将GET请求发送到此API网址 https://visblueiotfunctionapptest.azurewebsites.net/api/GetDeviceList
连同我拥有并要求正文的授权令牌
{
"UserEmail" : "xxxx@xxxxx.dk", //this can be null
"FromDateUTC" : "2012-04-23T18:25:43.511Z"
}
在邮递员中,这就像一个护身符,我可以随意获取数据。问题是,当我尝试从前端(react-app)或其他任何地方(任何其他API环境,如Postman)执行相同的操作时
我总是收到400个错误的请求,这使我认为我传递给体内的数据有问题。
我试图对所有请求使用不同的发送请求的方式(AXIOS,Fetch,XML,Ajax)相同。我什至尝试从Postman生成片段,但一无所获。
这是我的请求代码的示例
var data = "{\"Usermail\":\"cc@ccc.dk\",\"FromDateUTC\":\"2012-04-23T18:25:43.511Z\"}";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("GET", "http://visblueiotfunctionapptest.azurewebsites.net/api/GetDeviceList");
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.setRequestHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik4tbEMwbi05REFMcXdodUhZbkhRNjNHZUNYYyIsImtpZCI6Ik4tbEMwbi05REFMcXdodUhZbkhRNjNHZUNYYyJ9.eyJhdWQiOiIzMDk5OGFhZC1iYzYwLTQxZDQtYTYwMi03ZDRjMTRkOTU2MjQiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC8zNWNhMjFlYi0yZjg1LTRiNDMtYjFlNy02YTlmNWE2YzBmZjYvIiwiaWF0IjoxNTUzMDY3Mjk3LCJuYmYiOjE1NTMwNjcyOTcsImV4cCI6MTU1MzA3MTE5NywiYWNyIjoiMSIsImFpbyI6IkFVUUF1LzhLQUFBQStveUIvZmpUV0p1SzFFYVo5elBJbmlXbjRnVVEwL20vTkJXK3V6Y2JvRXhpZnhyZWpWbHpZ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"ffe68215-e89d-4847-8f12-deef46d44393");
xhr.send(data);
我在这里做错什么,我的请求与Postman的处理方式有什么区别?
编辑
答案 0 :(得分:1)
使用GET请求时,您无法发送请求正文。还将内容类型设置为application/json
尝试POST请求:
var data = {
"UserEmail": "xxxx@xxxxx.dk",
"FromDateUTC": "2012-04-23T18:25:43.511Z"
}
var url = "http://visblueiotfunctionapptest.azurewebsites.net/api/GetDeviceList";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.setRequestHeader("Authorization", "Bearer *your secret token*");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.send(JSON.stringify(data));
编辑
如果必须是GET请求,则可以在查询字符串中发送数据:
var query_string = "UserEmail=" + encodeURIComponent("xxxx@xxxxx.dk") + "&FromDateUTC=" + encodeURIComponent("2012-04-23T18:25:43.511Z")
var url = "http://visblueiotfunctionapptest.azurewebsites.net/api/GetDeviceList";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("GET", url + "?" + query_string);
xhr.setRequestHeader("Authorization", "Bearer *your secret token*");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.send();
EDIT2
使用GET方法发送请求正文似乎是有效的。但是不鼓励这样做。有关详细信息,请参见此stackoverflow答案。
根据MDN,当方法为GET
或HEAD
时,XMLHttpRequest API只会忽略任何正文。
您的邮递员似乎将请求正文包含在GET
方法中,并且调用成功。
您会遇到两种可能性:
GET
(带查询字符串)或POST
(带请求正文)。当API 仅仅接受GET
和主体有效载荷时,您将不能通过XMLHttpRequest使用此API。