1

i was away from Ruby for a while...now, i need to recreate a php api connection (curl) on ruby, to add it to my rails app, but all im getting is a Net::HTTPMovedPermanently message

here is the original php version:

$url = "https://marketplace.walmartapis.com/v3/token"; $uniqid = uniqid(); $authorization_key = base64_encode($client_id.":".$client_secret); $code = ""; $ch = curl_init(); $options = 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); if($code == 201 || $code == 200) { $token = json_decode($response,true); return $token['access_token']; } } 

And this is my Ruby version so far:

require 'net/http' require 'base64' client_id = "id" client_secret = "secret" url = "https://marketplace.walmartapis.com/v3/token/" uniqid = "1234567890abc" uri = URI(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' http = Net::HTTP.new(uri.host) response = http.request(request) puts response 

What im missing?

Update...

i've changed some things based on curl-to-ruby web, and is at follows...now im getting a 400 error

require 'net/http' require 'uri' client_id = "foo" client_secret = "bar" url = "https://marketplace.walmartapis.com/v3/token" uniqid = "1234567890abc" 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" req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, req_options) do |http| http.request(request) end puts response.code puts response.body 

i created a new question to clear out whats working so far, and the missing fields comparing to php

3
  • I am surprised the cURL version works at all - because what is fed to curl_setopt_array there, are not valid cURL options names to begin with. Looks like all of those should actually be passed as headers. Commented Aug 25, 2020 at 8:13
  • “but all im getting is a Net::HTTPMovedPermanently message” - and what is the actual target URL of that redirect, what does the Location header contain? Commented Aug 25, 2020 at 8:13
  • is the same address as the URL, which now i believe i get why, i wasnt using https, so it was redirecting me to https... when i added the use_ssl: uri.scheme == "https" its stopped redirecting, but now giving me a 400 error, so the packet im sending is wrong... not sure why though Commented Sep 6, 2020 at 15:21

1 Answer 1

2

It looks like the resource you are trying to access has been moved. The HTTPMovedPermanently reponse is a form of HTTPRedirection. Your code should make another request to the new location.

This is the example directly from the Net::HTTP documentation:

def fetch(uri_str, limit = 10) # You should choose a better exception. raise ArgumentError, 'too many HTTP redirects' if limit == 0 response = Net::HTTP.get_response(URI(uri_str)) case response when Net::HTTPSuccess then response when Net::HTTPRedirection then location = response['location'] warn "redirected to #{location}" fetch(location, limit - 1) else response.value end end 
Sign up to request clarification or add additional context in comments.

5 Comments

but the php version works without any redirects... so my guess is that something is not being called properly
Some libraries automatically follow redirects for you. Net::HTTP does not. I suggest you try to follow the example.
I looked at your PHP code a little bit more closely and see that you are using some curl based library to make the request. The command line version of Curl does not follow redirects by default but can be configured to do so. My guess is that the PHP curl library you're using configures curl to follow redirects.
no, it didnt work with the redirect function either... i believe there is something wrong in the ruby code itself
The response['location'] is the same url addresss redirected to marketplace.walmartapis.com/v3/token marketplace.walmartapis.com/v3/token so its taken some parameter wrong to redirect to the actual uri that we added

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.