admin管理员组文章数量:1415645
I've been trying to make a Lyrebird application work. Mind that I only have basic javascript/php knowledge and have never done this so I tried implementing a cURL request noted on: "". Needlessly to say that it doesn't work in javascript as well as PHP (even though I looked up on how to do it?)
I only need to run this example (the details are fake):
# Request #
curl -H 'Content-Type: application/json'
'' -d
'{
"grant_type": "authorization_code",
"client_id": "19qV2jZy1G44ifOxk6kgowAt9F0",
"client_secret": "19qnfRvIXdmQKhSbLG0CLxng5Mz",
"code": "19qozJe3hwnPvfl5xyNuR3MJ1NK"
}'
# expected Response #
{
"access_token": "18QdNlaDvkzMbgQ5SXmKNGmexWo"
}
How do I run the request (programming language?) in a way I get the "Expected Response" noted in the example?
I've been trying to make a Lyrebird application work. Mind that I only have basic javascript/php knowledge and have never done this so I tried implementing a cURL request noted on: "http://docs.lyrebird.ai". Needlessly to say that it doesn't work in javascript as well as PHP (even though I looked up on how to do it?)
I only need to run this example (the details are fake):
# Request #
curl -H 'Content-Type: application/json'
'https://avatar.lyrebird.ai/api/v0/token' -d
'{
"grant_type": "authorization_code",
"client_id": "19qV2jZy1G44ifOxk6kgowAt9F0",
"client_secret": "19qnfRvIXdmQKhSbLG0CLxng5Mz",
"code": "19qozJe3hwnPvfl5xyNuR3MJ1NK"
}'
# expected Response #
{
"access_token": "18QdNlaDvkzMbgQ5SXmKNGmexWo"
}
How do I run the request (programming language?) in a way I get the "Expected Response" noted in the example?
Share Improve this question edited Nov 13, 2018 at 15:24 RickyR asked Nov 13, 2018 at 15:20 RickyRRickyR 811 silver badge7 bronze badges 3- 1 You have tagged this with both php and node.js. Are you using both? Several different request libraries you can use in node for this and php also has cUrl extension and Guzzle is helpful library in php – charlietfl Commented Nov 13, 2018 at 15:23
- It's a program you run from a shell (at least, your example is): curl.haxx.se/docs/manual.html – mikeb Commented Nov 13, 2018 at 15:26
- I honestly have no clue which tools to use. I use standard javascript and am using node for this project probably. But now I'm just figuring out how to get my access token – RickyR Commented Nov 13, 2018 at 15:26
3 Answers
Reset to default 3curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The mand is designed to work without user interaction. -- https://curl.haxx.se/docs/manpage.html
If I understood your question, what you´re trying to achieve is transform curl into your favorite (PHP) language?
There are many different ways to do this but here are my 2 favorites:
Postman
Postman allows you to import curl mands for later manipulation and also once your mand has been imported you can actually generate code snippets from a large list of supported languages including PHP.
Curl-to-PHP
This website has a copy-paste transformation approach.
curl is a linux-mand to execute an http request to an url from mand line. The tutorial, from which you posted the code, is just an example which creates a http-post request to the url.
You should check the API of your programming lanuage (PHP or node.js) for how to do a http-post request. Here is a question for how to make an http post from node.js: How to make an HTTP POST request in node.js?
In js you can use e.g. fetch:
var data={
grant_type: "authorization_code",
client_id: "19qV2jZy1G44ifOxk6kgowAt9F0",
client_secret: "19qnfRvIXdmQKhSbLG0CLxng5Mz",
code: "19qozJe3hwnPvfl5xyNuR3MJ1NK"
}
fetch('https://avatar.lyrebird.ai/api/v0/token', {
method: 'POST',
headers: { "Content-Type": "application/json" },
credentials: 'include',
body: JSON.stringify(data)
}).then(function(res) {
return res.json();
}).then(function(res){
console.log(res);
}).catch((e)=>{alert (e)})
本文标签: javascriptWhat is a quotcURLquot and how do I execute itStack Overflow
版权声明:本文标题:javascript - What is a "cURL" and how do I execute it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745241417a2649349.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论