jQuery, How do I format a JSON request with authorization? - javascript

I'm new to JSON requests, cURL, etc. and I can't figure out how to correctly format a JSON request with authorization. I'm trying to call on a Spotify API and they reccomend a cURL request:
GET https://api.spotify.com/v1/search
and
curl -X "GET" "https://api.spotify.com/v1/search?q=tania%20bowra&type=artist" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer ReallyLongKey"
I don't know how to use cURL and GET so I tried to use ajax:
function Search() {
$.ajax({
url: 'https://api.spotify.com/v1/search?q=Muse&type=track%2Cartist&market=US&limit=10&offset=5',
datatype: 'json',
type: 'get',
cache: false,
success: function(Data) {
//stuff
}
});
}
How do I use cURL or add authentication to the ajax method? Thanks in advance,
Justin.

Related

How do I transform this curl request with javascript axios

I want to perform a request to the Google speech API, to return the translation of audio I sent to the API
If I use the curl command below, I successfully retrieve the data, but I don't know how to pass it to an Axios request.
curl command:
curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) -H "Content-Type: application/json; charset=utf-8" "https://speech.googleapis.com/v1/operations/{speech_name}"
// creating axios
const api = axios.create({
baseURL: https://speech.googleapis.com/v1/operations,
crossDomain: true,
responseType: 'json',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization':'your_key
},
});
// calling api
api.get('/speech_name')
.then(res=>{
console.log(res);
}).
catch('error');
Note : you can use other request method also api.post(url)

converting curl query in jquery ajax request

I have a curl command for getting json data and I want to send this request from javascript program. Please anybody can tell how to convert this curl command into jquery ajax request.
Command:
curl -H "Snapdeal-Affiliate-Id:<your affiliate ID>" -H "Snapdeal-Token-Id:<your affiliate Token>" "<URL for the chosen category>" -H "Accept:application/json"
cURL -H parameters add extra headers to the request.
jQuery.ajax have an option headers, this is a Plain JavaScript Object with the pair name/value for each header.
Then, your cURL request will translate in something like this:
$.ajax(url, {
url: "<URL for the chosen category>"
headers: {
"Snapdeal-Affiliate-Id":"<your affiliate ID>",
"Snapdeal-Token-Id":"<your affiliate Token>"
},
accepts: "application/json"
}).done(function (data) {
// code to handle succesful response
}).fail(function (data) {
// code to handle error response
});

Ajax POST corresponding to a curl POST command

I have this command:
curl -vX POST --header "Content-Type: text/json" --header "Authorization:MediaPreCompute" -d #someFile.txt someURL
someFile.txt has the data to be posted.
say it has:
{
keys: [],
observer: "someobserver",
table: "soneTable"
}
How can I do the same post request in JavaScript using Ajax. I know it would be something like this.. but how can I send the header: Authorization?
$.ajax({
type: "POST",
contentType: "application/json",
url: 'someUrl',
data: {myJsonDataInFile}
});
And also if the curl command gives me some output on terminal after completing, How can I collect that output while doing the same using Ajax.
Thanks in advance.

REST call Javascript

How do I make a call to a REST Api with Javascript (or ajax or jquery)?
curl -v -X PUT -H "Content-Type: application/json" -H "Accept: application/json" -X PUT --user user:password http://url -d "{\"name\": \"Marcus0.2\",\"start\": 1361381326000,\"end\": 1361640526000}"
I need help converting this to a useable ajax call. Thanks
$.ajax({
type: 'put',
url: 'https://this.is.my/url',
dataType: 'json',
data: { \"name\": \"Marcus0.3\" , \"start\": 500000 , \"end\": 1361640526000 }
});
This code does not work [not sure why]. Also, 'https://this.is.my/url' is just short for the longer url, which I don't want to post online
No need to add escaping to your data element... you can send a whole object and jQuery will take care of it for you.
$.ajax({
type: 'put',
url: 'https://this.is.my/url',
dataType: 'json',
data: {
name: 'Marcus0.3',
start: 500000,
end: 1361640526000
}
});

how to transform from curl command to ajax request

I have command to curl to server to get information
curl -v -H "Content-Type:application/json" -H "X-KGP-AUTH-TOKEN: a5a95c30274611e2ae10000c29bb7331" -H "X-KGP-APPID:id.kenhgiaiphap.kcloud" -H "X-KGP-APPVER:0.0.1" -H "X-KGP-DEVID:xxx" -H "X-KGP-DEVTYPE:xxx" http://test.kenhgiaiphap.vn/kprice/account/profile/get/token
I write ajax to handle this
$.ajax({
url: "http://test.kenhgiaiphap.vn/kprice/account/profile/get/token",
type: "POST",
cache: false,
dataType: 'json',
success: function() { alert('hello!'); },
error: function(html) { alert(html); },
beforeSend: setHeader
});
function setHeader(xhr) {
xhr.setRequestHeader('X-KGP-AUTH-TOKEN','a5a95c30274611e2ae10000c29bb7331');
xhr.setRequestHeader('X-KGP-APPVER', '0.0.1');
xhr.setRequestHeader('X-KGP-DEVID', 'xxx');
xhr.setRequestHeader('X-KGP-APPID','id.kenhgiaiphap.kcloud');
xhr.setRequestHeader('X-KGP-DEVTYPE', 'xxx');
}
But I have problem is
2XMLHttpRequest cannot load http://test.kenhgiaiphap.vn/kprice/account/profile/get/token. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
and in request is
token
test.kenhgiaiphap.vn/kprice/account/profile/get
OPTIONS
(canceled)
Load cancelled
text/plain
jquery-1.7.2.min.js:2320
Script
156B
0B
1.15s
39ms
39ms1.11s
Thank for support!
This is a browser issue.
Change dataType to jsonp or add callback=? to your url:
http://test.kenhgiaiphap.vn/kprice/account/profile/get/token?callback=?
Future refer to https://stackoverflow.com/a/6396653/744255
You cannot use post in client site "Same Origin Policy issue".
Ou can use jsonp instead 'json' and change to get, pretty much following "Gabriel Santos" suggestion

Categories

Resources