Converting curl commands to http request interface - r

How would I convert the following curl commands from the Lyft api to http interfaced requests (so they can be executed over web like https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY)? If http requests translation is not possible, how would I integrate and process these curl commands in R?
#Authentication code
curl -X POST -H "Content-Type: application/json" \
--user "<client_id>:<client_secret>" \
-d '{"grant_type": "client_credentials", "scope": "public"}' \
'https://api.lyft.com/oauth/token'
#Search query
curl --include -X GET -H 'Authorization: Bearer <access_token>' \
'https://api.lyft.com/v1/eta?lat=37.7833&lng=-122.4167'

Hi you could use https://curl.trillworks.com/ to convert curl commands to the language of your choice or you could use lyft SDK's (for Python use https://pypi.python.org/pypi/lyft_rides).
Here is the corresponding Python version
import requests
headers = {
'Content-Type': 'application/json',
}
data = '{"grant_type": "client_credentials", "scope": "public"}'
requests.post('https://api.lyft.com/oauth/token', headers=headers, data=data, auth=('<client_id>', '<client_secret>'))
From this post request you will get access token that has to be used for subsequent requests.
headers = {
'Authorization': 'Bearer <access_token>',
}
requests.get('https://api.lyft.com/v1/eta?lat=37.7833&lng=-122.4167', headers=headers)
Note: I haven't tested this as I am unable to create a lyft developer account so there might be some minor changes in the code given here.

[Full disclosure: I'm a Developer Advocate at Lyft] I'm not very familiar with R, but could you integrate the responses/calls using the method described in this blog post?
https://www.r-bloggers.com/accessing-apis-from-r-and-a-little-r-programming/

ReqBin can automatically convert Curl commands to HTTP requests
An example of such request:
Convert Curl to HTTP Request

Related

Why firebase emulator function says request body is missing data?

I'm running my local emulator suite and I'm constantly getting error messages for my curl requests. The following command:
curl -X POST http://localhost:5001/my-project/us-central1/myFunction \
-H "Content-Type: application/json" \
-d '{"productId": 123456, "quantity": 100}'
Is always showing this in the emulator CLI:
> {"productId":123456,"quantity":100,"severity":"WARNING","message":"Request body is missing data."}
> {"severity":"ERROR","message":"Invalid request, unable to process."}
None of the code was executed in the function as it starts with a console log which is never printed here. Any thoughts?
That error occurs when you use the onCall method. So, I would assume that you are using functions.https.onCall. As explained in this documentation:
It's important to keep in mind that HTTPS callable functions are similar but not identical to HTTP functions. To use HTTPS callable functions you must use the client SDK for your platform together with the functions.https backend API (or implement the protocol).
If you want to directly call the function via its endpoint then you should follow the protocol specification for https.onCall. One of its requirements is the request body should be a JSON object with data as its main key.
An example request JSON object should be like this instead:
{
"data": {
"productId": 123456,
"quantity": 100
}
}
For reference, here's the full curl request:
curl -X POST http://localhost:5001/my-project/us-central1/myFunction \
-H "Content-Type: application/json" \
-d '{ "data": { "productId": 123456, "quantity": 100 } }'
For more information, you may check out this documentation:
Protocol specification for https.onCall

What does -d stand for in curl request?

I am trying to send this HTTP request in Postman application:
curl -v https://api.someurl.com/z1/lists \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: authorization" \
-d '{ "list_id": "DXVBDAD" }'
Any body knows what -d stands for? and where should I put it in Postman?
The documentation says this:
(HTTP) Sends the specified data in a POST request to the HTTP server[...]
So this will be the body of your POST request. In Postman you have to put it into the 'body' field. There select 'raw' and then select 'application/json'.
Because that's the Content-Type of your request, specified with -H.
The -d or --data option makes the curl command send data in POST request to the server. This option makes the curl command pass data to the server using content-type (JSON in your case) just as the browser does when a user submits a form.

Configure HTTP Post headers and body to send Twilio request

I am trying to get my custom built HTTP Post request to execute the following call to twilio
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/xxxxx/Messages.json' \
--data-urlencode 'To=+15558675309' \
--data-urlencode 'From=+15017250604' \
--data-urlencode 'Body=This is the ship that made the Kessel Run in fourteen parsecs?' \
-d 'MediaUrl=https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg' \
-u xxxxx:your_auth_token
My custom built request accepts an URL, headers and a body, similar to the HTTP Post form at Hurl.it.
How do I translate the portions
--data-urlencode
and
-d
into my URL, headers and body?
Example: The -u part I put into a header with "Authorization: Basic ", and it worked perfectly, the server recognises me. I just can't get it to recognise the From, To, Body parts.
Tks!
Twilio developer evangelist here.
Both the -d and --data-urlencode flags indicate adding data to the POST request body. The data also needs to be url encoded. When the request is made, all the url encoded data is concatenated with ampersands and sent as the body.
In the case of your example, the POST request body would end up looking like this:
To=%2B15558675309&From=%2B15017250604&Body=This%20is%20the%20ship%20that%20made%20the%20Kessel%20Run%20in%20fourteen%20parsecs%3F&MediaUrl=https%3A%2F%2Fc1.staticflickr.com%2F3%2F2899%2F14341091933_1e92e62d12_b.jpg
Let me know if that helps at all.

Pass SSOTokenID as set-cookie value in OpenAM

I'm using openam OAuth/OpenID for user authentication. As mentioned in the documentations, I could get SSOTokenID as a JSON object by making following HTTP request.
curl -X POST -H "X-OpenAM-Username: demo" -H "X-OpenAM-Password: changeit" -H "Content-Type: application/json" -d '' -k -v https://openam.example.com:8443/openam/json/authenticate?realm=/
Instead of that, I want to get SSOTokenID as the Set-Cookie header value of the HTTP response. Are there anyway that i can do it?
Assuming you are only using an authentication module that accepts a NameCallback and PasswordCallback (as you used in your example), then you can just use the legacy UI zero-page login , you need to disable XUI though
Using your example
curl -X POST -d 'IDToken1=demo&IDToken2=changeit' -k -v https://openam.example.com:8443/openam/UI/Login?realm=/

What is curl doing with -d and -X GET?

I'm looking to this snippet of code:
curl -X GET 'https://api.newrelic.com/v2/applications/1622/metrics/data.json' \
-H 'X-Api-Key:30f4ec24a1f7dd9998a536b05840b17f7d42c7c1' -i \
-d 'names[]=EndUser&names[]=EndUser/Apdex&values[]=call_count&values[]=average_response_time&values[]=score&summarize=true'
from "Listing your app ID and metric data".
But curl's man page only talks about -d/--data in the context of POST requests, so, what's really happening here in terms of the HTTP request sent to the server?
-d with GET request just sends a query string, however the endpoint where data are sent must be set to consume application/x-www-form-urlencoded content type - have just checked that.
In general it's weird and I wouldn't implement it in such a way.
When such query is sent to java servlet - the body is accessible via.. getInputStream() method [sic!].

Resources