Axios post spotify api request return 400 - reactjs

I am using spotify api for a project. Every request I made work fine except this one. I want to be able to create a new playlist. The scope was already checked, it's not the source of the problem. For this I made the following request
axios('https://api.spotify.com/v1/users/' + ID + '/playlists', {
method: 'POST',
headers: {
'Authorization' : 'Bearer ' + data.data.access_token,
"Content-Type" : "application/json"
},
data: {
name: Name,
Description: Description
}
})
when I tried it, I've got 400 Bad Request - The request could not be understood by the server due to malformed syntax. The message body will contain more information
Here's the link for the spotify api references
https://developer.spotify.com/documentation/web-api/reference/playlists/create-playlist/

assuming your ID is defined. You need to stringify your json.
Been a while since I used axios,
axios('https://api.spotify.com/v1/users/' + ID + '/playlists', {
method: 'POST',
headers: {
'Authorization' : 'Bearer ' + data.data.access_token,
'Content-Type': 'application/json'
},
data: JSON.stringify({
name: Name,
description: Description
})
})

Related

Spotify WebPlayer API - Change Player Request

I've been working on a Spotify player using React, and so far I've been able to get the player to properly connect to the API and receive a device_id.
My next step is change the player to the newly "made" one, but I keep on receiving 400: Required field device_ids missing, even though I have it included in the PUT request:
const requestOptions = {
method: 'PUT',
headers: {
'Authorization' : 'Bearer ' + spotifyAccessToken
},
data: {
"device_ids": [device_id],
"play": 'true'
}
};
// Change player
fetch('https://api.spotify.com/v1/me/player', requestOptions)
Am I missing something/formatting it incorrectly? I've tried changing the data to body and I instead get a 400: Malformed JSON.
Thanks in advance!
I was able to get this working using the following format:
const requestOptions = {
method: 'PUT',
body: JSON.stringify({
device_ids: [southORadioDevId],
play: 'true'
}),
headers: {
Authorization: `Bearer ${spotifyAccessToken}`
},
};
// Change player
fetch('https://api.spotify.com/v1/me/player', requestOptions)

Napster API Auth - BAD Request / Unauthorized

I am coding in Reactjs and trying to Auth/Outh into the Napster Web API, followed the information on this page: https://developer.napster.com/api/v2.2#authentication
A sample of my current code:
const API_KEY = 'OWIxMjhlY2MtOTA3Yi00NWJiLThiYTktODc3OTNiYTQ4MGU4';
const API_KEY_SECRET = 'OWIxMjhlY2MtOTA3Yi00NWJiLThiYTktODc3OTNiYTQ4MGU4';
url: 'https://api.napster.com/oauth/access_token',
method: 'post',
params: {
client_id: API_KEY,
client_secret: API_KEY_SECRET
},
headers: {
'Accept':'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + (new Buffer(API_KEY + ':' + API_KEY_SECRET).toString('base64'))
},
data: querystring.stringify({ grant_type: 'authorization_code' })
Response Payload:
{"code":"UnauthorizedError","message":"Authentication code not valid"}
{"code":"BadRequestError","message":"Invalid grant_type parameter"}
According to the documentation and sample below....
curl -v -X POST -d "client_id={api_key}&client_secret={api_secret}&response_type=code&grant_type=authorization_code&redirect_uri={redirect_uri}&code={temporary_code}" "https://api.napster.com/oauth/access_token"
...You don't need to send Authorization headers. It's a normal form post with all the parameters. So If you include the rest of the parameters along with client_id : API_KEY... it should do the trick.

Getting error while creating peyment(Stripe)

I'm using this code to create payment(Stripe) in the tab Pre-request script
pm.sendRequest({
url: 'https://api.stripe.com/v1/tokens',
method: 'POST',
headers: {
'Authorization': 'Bearer pk_test_...',
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
'card[number]': 2223003122003222,
'card[exp_month]': 12,
'card[exp_year]': 2020,
'card[cvc]': 123
}
And getting this error:
You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY').
everything works as a separate request
In my case, I had my dotenv import come after I was calling my secret key.
const stripe = require("stripe")(process.env.STRIPE_SECRET);
require ("dotenv").config();
Reversing those two lines of code was what I needed to do.
require ("dotenv").config();
const stripe = require("stripe")(process.env.STRIPE_SECRET);
I think you're after something like this:
pm.sendRequest({
url: 'https://api.stripe.com/v1/tokens',
method: 'POST',
header: {
'Authorization': 'Bearer pk_test_......',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: {
mode: 'urlencoded',
urlencoded: [
{ key: "card[number]", value: 2223003122003222 },
{ key: "card[exp_month]", value: 12 },
{ key: "card[exp_year]", value: 2020 },
{ key: "card[cvc]", value: 123 },
]
}
})
Your issue was around the headers key as it should be header. Also, the way the payload was written seemed like it would error(?). I tried this code locally and saw the response back from Stripe.
You might want to change the token credentials now that you've posted it here.

Sending POST request weird issue

I'm quite new with ANGULAR and web development in general and I'm currently working on a web tool. Now I want this tool to send a POST request to a web service but Im encountering a weird bug. Now I have below code in my javascript:
var data_info = { test_id: 'TEST', model_id: 'TEST:TEST_ID' };
//data_info = JSON.stringify(data_info);
var request_json = {
method: 'POST',
url: url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: data_info,
cache: false,
};
console.log(request_json);
$http(request_json).then(function successCallback(response) {
// response code here
}
Now this code currently doesn't pass the preflight request check and is always returning a 405. But if I change the line data: data_info in the request JSON into a new key let's say body: data_info it now successfully sends the request and I can confirm that the service is receiving it. I'm not sure what's the issue here and can't figure it out.
change your header to :
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
Please try

Meteor Jira POST call

I'm trying to adjust the transition (the status) of an issue in Jira with a POST call. I'm running a Meteor application with a Meteor Backend and a Angular frontend.
Meteor.methods({
performPostCall: function(id, status){
var transitionObject = {
"transition": {
"id": "11"
}
};
var result = HTTP.call('POST', 'https://privateurl/rest/api/2/issue/' + id + '/transitions',
{data: transitionObject},
{headers :{
'Access-Control-Allow-Origin': '*',
'Authorization': 'Basic ******',
'Content-Type': 'application/json',
'Accept': 'application/json'
}});
return result;
}
});
In my frontend, I call the meteor method and handle the callback like this:
Meteor.call('performPostCall', id, status, function(error, success){
if(error){
alert(error);
} else {
alert(success);
}
});
When I press the button that fires the meteor method call, it will this strange and lovely error:
XMLHttpRequest cannot load https://ddp--0266-remoteserverurl/sockjs/info?cb=4bxmv_smgg. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'remoteserverurl' is therefore not allowed access. The response had HTTP status code 503.
But I declared this 'Access-Control-Allow-Origin' in the header, so I don't understand why it is complaining. Can somebody tell me what I do wrong?
According to documentation third parameter should be an object with fields required to make a call, in your code data and headers are different objects.
Proper syntax would be :
HTTP.call('POST', 'https://privateurl/rest/api/2/issue/' + id + '/transitions', {
data: transitionObject,
headers :{
'Access-Control-Allow-Origin': '*',
'Authorization': 'Basic ******',
'Content-Type': 'application/json',
'Accept': 'application/json'
}});
Also, result variable is most likely undefined since you try to do it in synchronous fashion, POST call is asynchronous, make use of fourth parameter (callback) to pass data to client, like:
HTTP.call('POST', 'https://privateurl/rest/api/2/issue/' + id + '/transitions', {
data: transitionObject,
headers :{
'Access-Control-Allow-Origin': '*',
'Authorization': 'Basic ******',
'Content-Type': 'application/json',
'Accept': 'application/json'
}}, function (error, result) {
if (!error) {
return result;
}
});

Resources