最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is a "cURL" and how do I execute it? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 3

curl 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)})
发布评论

评论列表(0)

  1. 暂无评论