frenciaj
frenciaj

Reputation: 128

Convert Rest API call from PHP to RUBY

Im trying to convert a post connection to the walmart api, from php to ruby, this is the php version

$client_id = $data['client_id'];
        $client_secret = $data['client_secret'];
        $url = "https://marketplace.walmartapis.com/v3/token";
        $uniqid = uniqid();
        $authorization_key = base64_encode($client_id.":".$client_secret);
        $code = "";

        $ch = curl_init();
        $options = array(

                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_TIMEOUT => 60,
                CURLOPT_HEADER => false,
                CURLOPT_POST =>1,
                CURLOPT_POSTFIELDS => "grant_type=client_credentials",
                CURLOPT_HTTPHEADER => array(

                        "WM_SVC.NAME: Walmart Marketplace",
                        "WM_QOS.CORRELATION_ID: $uniqid",
                        "Authorization: Basic $authorization_key",
                        "Accept: application/json",
                        "Content-Type: application/x-www-form-urlencoded",
                ),
        );
        curl_setopt_array($ch,$options);
        $response = curl_exec($ch);
        $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        curl_close($ch);

and this is what i have so far:

url = "https://marketplace.walmartapis.com/v3/token/"
uniqid = "1234567890a1b"

uri = URI.parse(url)
request = Net::HTTP::Post.new(uri)
request["WM_SVC.NAME"] = "Walmart Marketplace"
request["WM_QOS.CORRELATION_ID"] = uniqid
request.basic_auth(client_id, client_secret)
request["Accept"] = "application/json"
request.content_type = "application/x-www-form-urlencoded"
request["WM_SVC.VERSION"] = "1.0.0"


req_options = {
  use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  http.request(request)
end

puts "error " + response.code
puts response.body

Now, im getting a 400 error, so something of the data im sending is incorrect or missing... comparing both, i dont have set the postfields option on ruby, for the POST request, not sure if the rest is required as well... any ideas?

Upvotes: 0

Views: 86

Answers (1)

Aetherus
Aetherus

Reputation: 8888

Try adding this line:

request.body = 'grant_type=client_credentials'

Upvotes: 1

Related Questions