This is my curl command which works nicely in Command line :
curl --data #order_new.json \
-H "X-Augury-Token:My_token_goes_here" \
-H "Content-Type:application/json" \
http://staging.hub.spreecommerce.com/api/stores/store_id_goes_here/messages
I need to implement the same in rails using any sort of Gem, Tried with HTTParty /rest_client / spree-api-client, but something wrong here :
require 'httparty'
result = HTTParty.post(
"http://staging.hub.spreecommerce.com/api/stores/52eb347f755b1c97e900001e/messages",
:body => JSON.parse(File.read("order_new.json")),
:header => {
"X-Augury-Token" => "UjjKsdxbrwjpAPx9Hiw4",
"Content-Type" => "application/json"
}
)
But I am getting Error,
"The page you were looking for doesn't exist (404)"
I need rails equivalent of above curl command, use of spree-api-client gem will be much helpful.
If you prefer to use the Spree::API::Client, could you post the results of your findings? Could you evaluate the output of the following commands and post back:
client = Spree::API::Client.new('http://staging.hub.spreecommerce.com/api/', 'UjjKsdxbrwjpAPx9Hiw4')
client.products.inspect
require 'httparty'
result = HTTParty.post(
"http://staging.hub.spreecommerce.com/api/stores/52eb347f755b1c97e900001e/messages",
:body => #order.to_json,
:header => {
"X-Augury-Token" => "UjjKsdxbrwjpAPx9Hiw4",
"Content-Type" => "application/json"
}
)
Don't parse json while passing to HTTParty body
Related
how to convert below curl request in httparty
curl --noproxy localhost -k -d '{"username":"admin", "password":"adminpass"}' -H "Content-Type: application/json" https://localhost:8443/api/authentication
got it working
require "httparty"
options =
{
:http_proxyaddr => nil,
:headers => {"Content-Type" => "application/json"},
:verify => false,
:body => {"username":"admin", "password":"adminpass"}.to_json
}
response = HTTParty.post('https://localhost:8443/api/authentication', options)
puts response.body
The fastbill API states in its docu to make this curl request in order to receive information:
curl -v -X POST \
–u {E-Mail-Adresse}:{API-Key} \
-H 'Content-Type: application/xml' \
-d '{xml body}' \
https://my.fastbill.com/api/1.0/api.php
Using RestClientI tried to translate this into a ruby like request:
How I read this:
- make a post request to https://my.fastbill.com/api/1.0/api.php using basic authentification and stating the content type in the header, correct?
Now this would be a resource based request in RestClient like this:
First I authenticate:
resource = RestClient::Resource.new( 'https://my.fastbill.com/api/1.0/api.php', 'my#email.de', 'API-KEY-XXXXX' )
which works and authorises me.
then putting my request in:
xml = '<?xml version="1.0" encoding="utf-8"?><FBAPI><SERVICE>customer.get</SERVICE><FILTER/></FBAPI>'
resource.post xml, content_type: 'application/xml'
It always returns 400 and I don't know what else to do here.
Also how would json work here?
resource.post param1: 'value', content_type: 'json'
would be obvious.
You can utilize the Restclient::Request.execute. 400 errors typically indicate that the request was not understood by the recipeint. This could be caused by the headers or malformed data. You may need to add the accept header. Try the example below
require 'rest_client'
RestClient::Request.execute(
method: :post,
user: 'api_user#example.com',
password: 'pass123',
url: "https://example/users.json",
headers: {content_type: :json, accept: :json},
payload: { 'user' => { 'name' => 'John Doe', 'email' => 'john.doe#example.com'} }.to_json,
)
You can find a detailed list of options here
$url="https://my.fastbill.com/api/1.0/api.php";
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print_r(json_decode($result));
I am trying to add merge field in Mailchimp V3 list with HTTParty but not able to convert curl to HTTParty format.
Curl Request format which is working fine :
curl --request POST \
--url 'https://usxx.api.mailchimp.com/3.0/lists/17efad7sd4/merge-fields' \
--user '12:d1c1d99dr5000c63f0f73f64b88e852e-xx' \
--header 'content-type: application/json' \
--data '{"name":"FAVORITEJOKE", "type":"text"}' \
--include
Httparty format with error API key missing
response = HTTParty.post("https://us12.api.mailchimp.com/3.0/lists/17efad7sde/merge-fields",
:body => {
:user => '12:d1c1d99dr5000c63f0f73f64b88e852e-xx',
:data => '{"name":"FAVORITEJOKE", "type":"text"}',
:include => ''
}.to_json,
:headers => { 'Content-Type' => 'application/json' } )
I also try it without include option but not working
There are several errors in your code.
curl user is the basic auth user, but you are passing it in the payload of the request
data is the payload, instead you are passing it as a node in your payload and then you double serialize it
include makes no sense there, it's not a payload item
This should be the correct version. Please take a moment to read the HTTParty and curl documentation and understand the differences.
HTTParty.post(
"https://us12.api.mailchimp.com/3.0/lists/17efad7sde/merge-fields",
basic_auth: { username: "12", password: "d1c1d99dr5000c63f0f73f64b88e852e-xx" },
headers: { 'Content-Type' => 'application/json' },
body: {
name: "FAVORITEJOKE",
type: "text",
}.to_json
)
I use the following curl command on the command line to generate a token id which I then use in the HTTParty code.
curl -d "username=Yoshi.Nosaka#test.com&password=testpwd" http://upload.test.org:8026/api2/auth-token/
The above curl command generates the token id:
6cb2997265877d4cee5467e93577fa
I then use the token id in HTTParty commands in my rails application. Example:
HTTParty.get("http://upload.test.org:8026/api2/repos/", :headers => { "Authorization" => "Token 6cb2997265877d4cee5467e93577fa"})
I would like to know how to generate that token id using HTTParty in my rails app, rather than doing it on the command line.
Thanks a lot for all your suggestions.
cURL -d option send data on a POST.
curl -d "username=Yoshi.Nosaka#test.com&password=testpwd" http://upload.test.org:8026/api2/auth-token/
I don't know if your app handles json requests. But if so, you can use something like the above.
HTTParty.post("http://upload.test.org:8026/api2/auth-token/",
{
:body => [ { "username" => "Yoshi.Nosaka#test.com", "password" => "testpwd" } ].to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
})
I'm having trouble converting a curl to a ruby script. Basically I'm trying to consume the parse.com RESTful api. Here is the curl
curl -X POST \
-H "X-Parse-Application-Id: XXX" \
-H "X-Parse-REST-API-Key: YYY" \
-H "Content-Type: application/json" \
-d '{
"channels": [
"Giants",
"Mets"
],
"data": {
"alert": "test message"
}
}' \
https://api.parse.com/1/push
And this Is what I've been trying to do (using HttParty)
puts "hello world"
require 'httparty'
require 'json'
class Parse
include HTTParty
base_uri "api.parse.com:443"
headers "X-Parse-Application-Id" => "XXX", "X-Parse-REST-API-Key" => "YYY", "Content-Type" => "application/json"
end
option = {:channels => "Giants", :data => {:alert => "un mensaje de mierda"}}
puts Parse.post("/1/push", body: option.to_json)
)
I'm getting an error code 107 saying invalid json. Any suggestions will be appreciated.
I think you just need to serialise the ruby structure to JSON in the second param. The param should be the string to POST, not a Ruby struct.
I think this will work (only other possible problem I can see is whether you'll connect via https as the code is written):
data = {"channels": ['Giants'], "data": {alert: 'un mensaje '}}
puts Parse.post("/1/push", body: data.to_json)
. . . the JSON-like format in the Ruby data structure is not JSON,
foo: "bar"
is just another Ruby (1.9+) way of saying
:foo => "bar"