Rails 5 dropping some keys in params which contains non null values - ruby-on-rails-3

I am moving from rails 3 to 5. I have a request to an API which accepts some parameter but is getting dropped in rails 5 for reasons I am not sure.
The params which I recieved in Rails 3 was as below:
params = {"Envelope" => {"Body" => {"notifications" => {"Notification" => {"id =>" XYZ, "sObject" => {"data1" => ABC, "data2" => PQR}}}}}, "action" => XXX, "controller" => YYY, "format" => "xml", "auth" => AUTH_TOKEN, "entity" => ENTITY_NAME}
Whereas in Rails 5, the params I am receiving is:
params = <ActionController::Parameters {"entity"=>ENTITY_NAME, "auth"=>AUTH_TOKEN, "format"=>"xml", "controller"=> YYY, "action"=> XXX} permitted: false>
How are the parameters getting dropped when the request is made?

The request was sending the body in XML format. Rails 3 parses the XML body by default to json but Rails 5 does not. actionpack-xml_parser gem has to be explicitly installed so that the request body is parsed.

Related

UnSupportedEntityException when trying to create/render records containing slug extension

I am trying to implement slugs in my bundle, but when i try to create a record and run my message consumer i get the following error:
Unexpected exception occurred during Direct URL generation ["exception" => Oro\Bundle\RedirectBundle\Exception\UnsupportedEntityException { …}] ["processor" => "Oro\Bundle\RedirectBundle\Async\DirectUrlProcessor","message_id" => "oro.6256de2124b630.96491198","message_body" => ["createRedirect" => true,"id" => [3],"class" => "Phpro\OroBundleBlogBundle\Entity\BlogPostCategory"],"message_properties" => ["oro.message_queue.client.topic_name" => "oro.redirect.generate_direct_url.entity","oro.message_queue.client.queue_name" => "oro.default","oro.security.token" => "organizationId=1;userId=1;userClass=Oro\Bundle\UserBundle\Entity\User;roles=ROLE_ADMINISTRATOR"],"message_headers" => ["content_type" => "application/json","message_id" => "oro.6256de2124b630.96491198","timestamp" => "1649860129","priority" => "2"],"elapsed_time" => "34 ms","memory_usage" => "107.57 MB"]
The slugs are stored, but the use of the get_slug_urls_for_prototypes twig filter results in the following error An exception has been thrown during the rendering of a template (""). Again because of the UnsupportedEntityException
Am i missing some configuration?
As mentioned in the OroCommerce documentation the entity must implement the interface to support slugs. Make sure you fulfilled this requirement.

eBay API in Perl - can't use SetShipmentTrackingInfoRequest to update tracking information

I'm trying to figure out why I can't seem to update a tracking number using the eBay API. Here is the page I'm referencing:
http://developer.ebay.com/DevZone/merchant-data/CallRef/SetShipmentTrackingInfo.html
Based on that, I've got the following code in Perl:
use Net::eBay;
my $ebay = new Net::eBay( {
SiteLevel => 'prod',
DeveloperKey => 'x',
ApplicationKey => 'xxxx',
CertificateKey => 'xxx',
Token => 'xxxx',
} );
$ebay->setDefaults( { API => 2, compatibility => 900 } );
my $result = $ebay->submitRequest( "SetShipmentTrackingInfoRequest",
{
DetailLevel => "ReturnAll",
ErrorLevel => "1",
SiteId => "1",
OrderID => 1234546, # not the real order ID I'm using :)
ShipmentTrackingDetails => {
ShipmentTrackingNumber => "12345",
ShippingCarrierUsed => "Hermes"
}
});
print $IN->header;
use Data::Dumper;
print Dumper($result);
When running it, I get an error in $result:
$VAR1 = {
'Errors' => {
'ErrorClassification' => 'RequestError',
'SeverityCode' => 'Error',
'ShortMessage' => 'Unsupported API call.',
'ErrorCode' => '2',
'LongMessage' => 'The API call "SetShipmentTrackingInfoRequest" is invalid or not supported in this release.'
},
'xmlns' => 'urn:ebay:apis:eBLBaseComponents',
'Timestamp' => '2016-10-21 07:03:04',
'Build' => '18007281',
'Version' => '900',
'Ack' => 'Failure'
};
I'm a bit confused, as it looks like SetShipmentTrackingInfoRequest is the API call I need to be making? I've not really done much with the eBay API, so it's possible I'm missing something stupid.
UPDATE: As per feedback below, I'm now using the CompleteSale API call:
http://developer.ebay.com/Devzone/XML/docs/Reference/eBay/CompleteSale.html
my $result = $ebay->submitRequest( "CompleteSale ",
{
DetailLevel => "ReturnAll",
ErrorLevel => "1",
SiteId => "1",
OrderID => 1933420817015,
Shipment => {
ShipmentTrackingDetails => {
ShipmentTrackingNumber => "77293124902615",
ShippingCarrierUsed => "Hermes"
}
}
});
When I run it, I now get the error:
'LongMessage' => 'XML Error Text: "; nested exception is:
org.xml.sax.SAXParseException: Attribute name "Request" associated with an element type "CompleteSale" must be followed by the
\' = \' character.".',
Enabling debugging, the XML being sent is:
<?xml version='1.0' encoding='utf-8'?>
<CompleteSale Request xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>xxxx</eBayAuthToken>
</RequesterCredentials>
<DetailLevel>ReturnAll</DetailLevel>
<ErrorLevel>1</ErrorLevel>
<OrderID>xxxxx</OrderID>
<Shipment>
<ShipmentTrackingDetails>
<ShipmentTrackingNumber>xxxx</ShipmentTrackingNumber>
<ShippingCarrierUsed>Hermes</ShippingCarrierUsed>
</ShipmentTrackingDetails>
</Shipment>
<SiteId>1</SiteId>
</CompleteSale Request>
From looking at the eBay API doc link you posted doesn't the second 'Note' entirely explain why it doesn't work as a single API call?
Note: SetShipmentTrackingInfo cannot be issued on its own like an ordinary API call, using an endpoint
It then goes on to say:
In the Trading API, the CompleteSale call provides similar functionality that you can invoke directly
So perhaps looking there should be the next step

Using SendCloud with 'X-SMTPAPI' in Rails

I hope this post will help someone.I need to use SendCloud to send emails through smtp.At the begin i add header in ActionMailer:
headers["X-SMTPAPI"] = JSON.dump({"to" => emails, "sub" => {"%name%" => names}})
But it can't work, and i also can't receiver the return error code through Rails.Then i find the way through communication with their support:
headers["X-SMTPAPI"] = Base64.encode64(JSON.dump({"to" => emails, "sub" => {"%name%" => names}}))
But it also can't work correctly.Then i compare the generated headers["X-SMTPAPI"] with the sent headers["X-SMTPAPI"], and found Rails insert '\n' in it for format.In the end, Mail gem convert the '\n':
def encode_crlf(value)
value.gsub!(CR, CR_ENCODED)
value.gsub!(LF, LF_ENCODED)
value
end
So, if i want it's success, i need to do like this:
headers["X-SMTPAPI"] = Base64.encode64(JSON.dump({"to" => emails, "sub" => {"%name%" => names}})).gsub!(/\n/,'')
Wow, i can send 'x-smtpapi' header in Rails successfully!
I solve it by encode and replacing '\n':
headers["X-SMTPAPI"] = Base64.encode64(JSON.dump({"to" => emails, "sub" => {"%name%" => names}})).gsub!(/\n/,'')

Why rack/test is combining hashes into one when performing POST or PUT operations

In my rspec test, I defined the following array of hashes and performed a POST:
body = {:event => { :invitations_attributes =>
[ {:recipient_id => 40}, {:email => 'a#a.com'}, {:facebook_id => 123456789} ] } }
post "#{#url}.json", body.reverse_merge(:auth_token => #token)
Based on the above, I expected the Rails server to receive "invitations_attributes" as an array of hashes. However, the developer.log file has the following:
Parameters: {"auth_token"=>"RSySKfN2L8b5QPqnfGf7", "event"=>{"invitations_attributes"=>
[{"recipient_id"=>"40", "email"=>"a#a.com", "facebook_id"=>"123456789"}]}}
(In the parameters above, "invitation_attributes" array contains only 1 hash.)
The following curl statement:
curl -X POST -H "Content-type: application/json" http://localhost:3000/api/v1/events.json -d '{"auth_token":"RSySKfN2L8b5QPqnfGf7","event":{"invitation_attributes":[{"recipient_id":40},{"email":"a#a.com"},{"facebook_id":123456789}]}}'
results in Rails' receiving the array of hashes intact, as evidenced by the log file entry below.
Parameters: {"auth_token"=>"RSySKfN2L8b5QPqnfGf7", "event"=>{"invitation_attributes"=>
[{"recipient_id"=>40}, {"email"=>"a#a.com"}, {"facebook_id"=>123456789}]}}
Rack/test is exhibiting this behavior for PUT operations as well as POST.
Why is rack/test combining the 3 hashes into 1 rather than sending the array exactly as it is defined? Is there a setting which will cause rack to exhibit the behavior I expected?
One workaround is to ensure that each hash contains each key by inserting nil value placeholder keys as follows:
body = {:event => { :invitations_attributes => [
{:recipient_id => 40, :recipient_email => nil, :recipient_facebook_id => nil},
{:recipient_email => user.email, :recipient_id => nil, :recipient_facebook_id => nil},
{:recipient_facebook_id => new_unused_facebook_id, :recipient_email => nil, :recipient_id => nil} ] } }
The hash above does cause the server to receive 3 separate hashes within the array. However, inserting placeholder keys is inconvenient and should not be required. Furthermore, scenarios where a controller acts differently based on the presence of a such a key (albeit uncommon), cannot be tested.

Why do I get a connection timeout for facebook api-video?

This is the API call Im attempting:
http://developers.facebook.com/docs/reference/rest/video.upload
(Video upload is not available in the new Graph API.)
I have tried many variations on the parameters. The code below is my best guess. If I modify the params to be obviously incorrect, change to http (not https) or try to use api.facebook.com for video, I get proper errors back.
However, my code below just waits a few minutes before reporting:
ETIMEDOUT: Connection timed out
Also included is working code to upload a photo - which is almost identical.
Ruby:
# Facebook Old-API method - testing only - this works.
def post_photo
url = "https://api.facebook.com/method/photos.upload"
body = {
nil => File.new(self.media.media_files.first.source_file, 'rb'),
:access_token => self.session.auth_data[:access_token],
:callback => "none",
:aid => "Test Photos",
:caption => "Test",
:uid => self.session.auth_data[:uid],
}
response = RestClient.post url, body
end
# Facebook Old-API method - doesn't work - connection timeout.
def post_video
url = "https://api-video.facebook.com/method/video.upload"
body = {
:nil => File.new(self.media.media_files.first.source_file, 'rb'),
:access_token => self.session.auth_data[:access_token],
:callback => "none",
:title => "Test title",
:description => "Test description",
:privacy => "{ value: 'EVERYONE' }",
:uid => self.session.auth_data[:uid],
}
response = RestClient.post url, body
end
PS: Im in Australia - is the API limited to eg the USA?
Thanks