Convert curl command to HTTParty Rails - ruby-on-rails-3

I'm currently trying to send a get request in HTTParty that logs in and gets some API data. I've successfully done this via curl by doing:
curl -v --user USERNAMEHERE:PassWordHere http://app.ionitnetworks.com/ionit-app/api/rest/v1/devices
This outputs in my console:
➜ HeatWatch git:(kunzig_new_heatwatch) ✗ curl -v --user (userOmitted):(password_omitted) http://app.ionitnetworks.com/ionit-app/api/rest/v1/devices
* Hostname was NOT found in DNS cache
* Trying 23.92.23.58...
* Connected to app.ionitnetworks.com (23.92.23.58) port 80 (#0)
* Server auth using Basic with user 'userOmitted'
> GET /ionit-app/api/rest/v1/devices HTTP/1.1
> Authorization: Basic ZG91Z0dofW301320Y2AsayZlLmNvbTpkb3VnMTIz
> User-Agent: curl/7.35.0
> Host: app.ionitnetworks.com
> Accept: */*
I've tried a bunch of stuff to get this to work in httparty but nothing has worked such as:
#response = HTTParty.post("https://app.ionitnetworks.com/ionit-app/api/rest/v1/devices.json", :headers =>{ "Authorization" =>"Token token=\"AccountsTokenOmitted\"" })
Any tips would be greatly appreciated!

I found that for me -u USERNAME:PASSWORD in curl is for the most part equivalent to basic_auth in HTTParty. I also like setting my options in a separate variable to make it a little easier to read/edit/adjust and then pass that variable to the call.
options = {
basic_auth: { username: USERNAME, password: PASSWORD }
}
HTTParty.post(<URL>, options)

Related

Kibana Rest API redirects to login

I'm new to ElasticSearch and Kibana. I'm trying to use Kibana's REST API, but the response i get are redirections to /login whatever the request i try.
The basePath has been modified in kibana.yml :
server.basePath: "/demo"
server.rewriteBasePath: true
Every request, such as :
$ curl -v -u user:passwd -X GET "127.0.0.1:5601/demo/api/features"
gets the following response :
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5601 (#0)
* Server auth using Basic with user 'user'
> GET /demo/api/features HTTP/1.1
> Authorization: Basic xxxxxxxx
> User-Agent: curl/7.38.0
> Host: 127.0.0.1:5601
> Accept: */*
>
< HTTP/1.1 302 Found
< location: /demo/login?nextUrl=/demo/api/features
< kbn-name: kibana
< content-type: text/html; charset=utf-8
< cache-control: no-cache
< content-length: 0
< connection: close
< Date: Thu, 26 Nov 2020 10:32:56 GMT
<
* Closing connection 0
I'm using v7.2.0 with Linux.
Could someone tell me what i'm doing wrong ?
Best regards
The problem came from the use of the plugin readonlyrest.
First, I had to log in and retrieve the cookie, before querying for anything :
curl -v -X POST "127.0.0.1:5601/demo/login" -d "username=user&password=passwd"-H "kbn-xsrf: true" -H 'Accept: application/json' -c cookie.txt
And then use with the cookie :
curl -v -X GET "127.0.0.1:5601/demo/api/features" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -H "Accept: application/json" -b cookie.txt
In more recent versions of Kibana and ROR we can obtain the cookie with:
curl '127.0.0.1:5601/login' -H 'kbn-xsrf: true' -H 'Content-Type: application/json' --data-raw '{"username":"user","password":"password"}' -c cookie.txt

how to send correct curl command to webserver

So I got the data that is being sent to a specific server. Now I want to do the same using curl from my local machine to play around with specific repsonses from the server and learn more about curl as well.
Here is part of my data
POST /auth HTTP/1.1
platform: android
X-Auth-Token: <censored>
Content-Type: application/json; charset=utf-8
Host: api.blabla.com
Accept-Encoding: gzip
And the data that is being sent:
{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}
Now when I try cURL in my dos shell, I try
curl --insecure -X POST https://api.blabla.com/auth --data '{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}'
The response I get from cURL is this:
{"code":401,"error":"blablaTokenRequired"}
Even though I specified the token. So there are two possible scenarios because the token is correct:
It has something to do with the SSL thing? (I use --insecure because I get an SSL error otherwise)
Something about my command is not correct but I can't figure out what.
Can someone kindly help me out? I am trying everything I can without success
I am not sure if I understand your application specific right, but probably one thing you need to take into account:
man curl says:
-d, --data <data>
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when
a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the
server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.
-d, --data is the same as --data-ascii. --data-raw is almost the same but does not have a special interpreta‐
tion of the # character. To post data purely binary, you should instead use the --data-binary option. To URL-
encode the value of a form field you may use --data-urlencode.
As I can't see in your example the necessity of sending data as HTML form input, probably your application expects just a "raw" POST body and then you have to try this:
curl --insecure -X POST https://api.blabla.com/auth --data--binary '{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}'
PS and for sure this is error is not about using --insecure which just asks curl to neglect ssl verification
you forgot the headers and enabling compressed encoding (gzip), however, i believe you can't force curl to only support gzip encoding using the curl command line alone, you will have to use libcurl, this will make the request say "Accept-Encoding: gzip,deflate" on most systems, using --compressed .. if that's not acceptable to you, rewrite it using libcurl (where you can force it to say only "gzip", if you wish, via CURLOPT_ENCODING )
curl -X POST https://api.blabla.com/auth --data '{"blabla_token": "sdsadsad", "blahblah_id": "23213", "locale": "us"}' --header 'platform: android' --header 'X-Auth-Token: <censored>' --header 'Content-Type: application/json; charset=utf-8' --header 'Host: api.blabla.com' --compressed
another gotcha: on some systems, there will be a default useragent header (like debian 6), while on some systems, curl comes without a default useragent (like debian 8).. you might want to use --user-agent '' too

Office 365 REST API to create contacts using CURL gets HTTPCode 400 Bad Request

I am testing Office 365 REST API using CURL following this link:
Contacts REST API in Office 365 APIs Preview
I can obtain correctly one contact using curl command in Windows like this:
curl --no-sessionid --insecure --basic --user "user#domain.com:password" -H "Accept: application/json" "https://outlook.office365.com/EWS/OData/Me/Contacts?$orderby=DisplayName+asc&$top=1"
And following documentation on this link if I try to create one contact using CURL with minimum required options for testing:
curl -X POST -d "{\"#odata.type\": \"#Microsoft.Exchange.Services.OData.Model.Contact\",\"GivenName\": \"TestContact\",\"EmailAddress1\": \"test#test.com\",\"BusinessPhone1\": \"123-456-7890\"}" https://outlook.office365.com/ews/odata/Me/Contacts --header "Content-Type:application/json" --insecure --verbose --user "user#domain.com:password"
I receive following error:
* About to connect() to outlook.office365.com port 443 (#0)
* Trying 157.56.250.178...
* connected
* Connected to outlook.office365.com (157.56.250.178) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
...
* SSL connection using ECDHE-RSA-AES256-SHA
* Server certificate:
...
* Server auth using Basic with user 'user#domain.com'
> POST /ews/odata/Me/Contacts HTTP/1.1
...
> Content-Length: 157
>
* upload completely sent off: 157 out of 157 bytes
< HTTP/1.1 400 Bad Request
...
< Content-Length: 82
<
{"error":{"code":"ErrorInvalidRequest","message":"Cannot read the request body."}}* Connection #0 to host outlook.office365.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
I have searched the internet and in stackoverflow but similar questions have no answer I'm looking for.
How could I create on contact using CURL?, I have tested it on Linux too but have the same results.
The following request works when I try it from Fiddler. Can you please try this out using CURL?
POST https://outlook.office365.com/ews/odata/Me/Contacts HTTP/1.1
Authorization: Basic <XXXX>
Content-Type: application/json
{
"GivenName" : "John",
"EmailAddresses" : [
{ "Address": "John#contoso.com", "Name" : "John" }
],
"BusinessPhones" : [
"123-456-7890"
]
}
I made a few changes to your request. You don't need to specify the OData.type as we infer that you are adding a Contact because you are sending a POST to Contacts collection. We need to fix our documentation as it lists the entity type as required. We have updated our namespace to Microsoft.OutlookServices and hence the type definitions have changed. To make the API easier to use, we have replaced EmailAddress1, EmailAddress2 etc. with a collection of EmailAddresses. Similarly, we have also changed BusinessPhones, HomePhones etc. to collections as well.
As I just explained in another post, the issues you are seeing are from some changes being rolled out to our preview APIs and our documentation is in the process of being updated. The current set of changes include versioning support, and this won't be an issue going forward.
Please let me know if you have any questions or need more info.
Thanks,
Venkat
Using following CURL command worked perfect:
curl -X POST -d "{\"GivenName\":\"John\",\"EmailAddresses\":[{\"Address\":\"John#contoso.com\",\"Name\":\"John\"}],\"BusinessPhones\":[\"123-456-7890\"]}" https://outlook.office365.com/ews/odata/Me/Contacts --header "Content-Type:application/json" --insecure --verbose --user "user#domain.com:password"
Now we can export contacts (this is not allowed from OWA) and import it using simple utilities like curl.
Thanks for your help Venkat.
Your original post showed the following namespace:
Microsoft.Exchange.Services.OData.Model.Contact
That has been changed to:
Microsoft.Office365.OutlookServices.Contact
Since the namespace was wrong, it couldn't read the request body.

GCM send message fails with 401 (Unauthorized)

I am trying to publish a message to GCM. But the send call fails with 401 status.
I am passing the Authorization header.
Passing the correct API key (with allow any IP).
I have Enabled the Cloud messaging service.
I have also tried using the browser key in place of API key(with no referrer)
The curl request looks like this >
curl -v --header "Authorization:key=VALID API KEY" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"r1\"]}"
POST /gcm/send HTTP/1.1
User-Agent: curl/7.24.0 (x86_64-redhat-linux-gnu) libcurl/7.24.0 NSS/3.13.5.0 zlib/1.2.5 libidn/1.18 libssh2/1.2.2
Host: android.googleapis.com
Accept: */*
Authorization:key= VALID API KEY With allow all IP
Content-Type:application/json
Content-Length: 28
Nothing seems to work. Any idea on why this happens?

WNS notification - receiving 404 even on valid token

I am trying to push notifications to my windows app via WNS,
I get a 404 response everytime.
I have verified that the Channel URI is valid, because every time I launch the app, I get the channel URI and use that immediately for testing.
Here is the curl call I make
curl -i -d "<tile><visual><binding template='TileSquareText01'><text id='1'>tile one text</text></binding><binding template='TileSquareText02'><text id='2'>tile two text</text></binding></visual></tile>" -H "Authorization: Bearer EgAeAQMAAAAEgAAACoAAFQOHLfF3CMYK/IUs8MgTUFoS9vwOM4AFavJ3TLGAczjxBJNm02+rMlWFGYNWAefkuCw/Qg62++O+5GdrLRqQCdBSSI+cZPPXwOKk6lagOLDXNzd41hPMG9/NYi/Li1dHddMLLkPVWuZ6Te+t1ofOeL30PDA9p8JVEVDAX/CfLXGNAFoAjQAAAAAAL+oNSH9RuFB/UbhQ60gEAA8AMTIzLjIwMS4xMzUuNTMAAAAAAF4AbXMtYXBwOi8vUy0xLTE1LTItMTIxMTkzNzMyNS0yNTg0NzczMzMzLTE1ODE1OTA0OTYtNDE4NTk0OTY5OS0yNDEwMjc4ODgzLTM4NjU2MTI0NzAtOTM5MDAzNTk0AA==" -H "content-type: text/xml" -H "content-length: 190" -H "X-WNS-Type: wns/tile" "https://sin.notify.windows.com:443/?token=AgYAAAA/Bcry6hlYw8kOU5f4LHaS+dycP7UxcsT45eUcNlxfznxCltMK3emgIsHRAiRUk1X5VnQ0h0FC7fbOno+QI5aqUt2Q05foUg+XYeFvIcdsp6wwoMJ8VEjyOBzcT+z1JNM="
I get the following response
HTTP/1.1 404 Not Found
Content-Length: 0
X-WNS-MSG-ID: 1FD852615FA1E9DA
X-WNS-DEBUG-TRACE: SINWNS2012431
Date: Fri, 30 Nov 2012 06:57:46 GMT
I've checked the headers are all correct, test this by placing a wrong header and getting 400 Bad request instead.
Here is my app code for getting the Channel URI
get_channel_uri :function(){
var channel;
var pushNotifications = Windows.Networking.PushNotifications;
var channelOperation = pushNotifications.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync();
return channelOperation.then(function (channel) {
if(channel.uri){
// send to server
}
},
function (error) {
}
);
}
Any ideas what am I missing?
I found the solution on the MSDN forums.
I had not done the following
in Visual Studio 2012 going to "Project->Store->Associate App With the Store..." and select the app you have created. This then updates the manifest for you installing the correct certificates etc. After doing that, my app created a different notify url and everything started working.