-1

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?

RickyR
  • 81
  • 1
  • 7
  • 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 Nov 13 '18 at 15:23
  • It's a program you run from a shell (at least, your example is): https://curl.haxx.se/docs/manual.html – mikeb Nov 13 '18 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 Nov 13 '18 at 15:26

3 Answers3

2

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 command 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 commands for later manipulation and also once your command has been imported you can actually generate code snippets from a large list of supported languages including PHP.

postman postman

Curl-to-PHP

This website has a copy-paste transformation approach. curl to php

Oscar Nevarez
  • 994
  • 12
  • 24
1

curl is a linux-command to execute an http request to an url from command 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?

Alex
  • 1,593
  • 1
  • 8
  • 12
0

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)})
MWO
  • 2,627
  • 2
  • 10
  • 25