[英]POST object to API, but API can't read it
我正在使用Axios將POST請求發送到Instagram API:
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.post('https://api.instagram.com/oauth/access_token', {
client_id: 'zzz',
client_secret: 'zzz',
grant_type: 'authorization_code',
redirect_uri: 'http://localhost:8080/oauth2',
code: 'zzz',
}).then(function(res) {
console.log("response ", res);
})
.catch(function(err) {
console.log("err ", err);
});
這遵循Instagram API文檔( https://www.instagram.com/developer/authentication/ )中給出的示例:
// curl -F 'client_id=CLIENT_ID' \
// -F 'client_secret=CLIENT_SECRET' \
// -F 'grant_type=authorization_code' \
// -F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
// -F 'code=CODE' \
// https://api.instagram.com/oauth/access_token
但是,返回以下錯誤:
data: {
code: 400,
error_type: 'OAuthException',
error_message: 'You must provide a client_id'
}
使用Chrome Postman時,沒有任何問題。 我收到授權令牌。 因此,顯然該API無法讀取我發送的內容,但我無法弄清楚原因。 我已經設置了標題:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, accept, authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
此外,我什至對http://jsonplaceholder.typicode.com/進行了相同的發布,並且按預期方式,我收到了正確的響應。
有什么想法我可能做錯了嗎?
編輯:
我只是注意到,當我使用Chrome Postman時,它僅適用於x-www-form-urlencoded設置。 我嘗試使用JSON和許多其他方式提交它,但收到以下相同的錯誤。
您可以使用以下代碼通過node.js應用程序中的axios發出application/x-www-form-urlencoded
請求:
var querystring = require('querystring');
axios.post('http://something.com/foo', querystring.stringify({
'bar': 123
});
請注意,您不需要設置Content-Type
標頭。 它將自動設置為application/x-www-form-urlencoded
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.