How do I use rest-client for ruby to post an image? - rest-client

I am trying to find a way to use the Postful API and I need to post an image in the body of the request. I am trying to use rest-client (but am open to other ways):
The error I get when I post is HTTP status code 422
Basically, I'm clueless and need guidance. :\
class PostfulsController < ApplicationController
require 'rest_client'
def submit
body = "<?xml version='1.0' encoding='UTF-8'?>
<mail>
<documents>
<document>
<template>
<source>gallery</source>
<name>Postcard: Image fit front</name>
</template>
<sections>
<section>
<name>Text</name>
<text>Hello, World!</text>
</section>
<section>
<name>Image</name>
<attachment>#{attachment_id}</attachment>
</section>
</sections>
</document>
</documents>
<addressees>
<addressee>
<name>John Doe</name>
<address>123 Main St</address>
<city>Anytown</city>
<state>AZ</state>
<postal-code>10000</postal-code>
</addressee>
</addressees>
</mail>"
result = RestClient.post 'http://www.postful.com/service/mail',
body, :content_type => 'text/plain'
end
end

Try
# result will contain the response
result = RestClient.post('http://www.postful.com/service/mail',
request.body,
{:content_type => 'text/plain',
'Authorization: Basic' => 'QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
p result.body
p result.code
p result.headers

Related

Amazon MWS submitFeed API Failure

I am trying to add product on amazon using MWS submitFeed. When i make API call it returns following error
SKU RGDSP*********, Missing Attributes product_type. SKU RGDSP********* does not match any ASIN. Creation of a new ASIN requires the following missing attributes: product_type. Feed ID: 0. For details, see http://sellercentral.amazon.in/gp/errorcode/200692370
If i add ProductType to the script with some value it thorws Invalid content was found starting with element 'ProductType' error.
Below is my XML code
<?xml version="1.0" encoding="iso-8859-1"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>A28XP9******</MerchantIdentifier>
</Header>
<MessageType>Product</MessageType>
<PurgeAndReplace>false</PurgeAndReplace>
<Message>
<MessageID>1</MessageID>
<OperationType>Update</OperationType>
<Product>
<SKU>SKU RGDSP*********</SKU>
<ProductTaxCode>A_GEN_NOTAX</ProductTaxCode>
<DescriptionData>
<Title>Full Sleeves Kurta Pyjama Set For Men</Title>
<Brand> Brand </Brand>
<Description>Look your ethnic best when you adorn this kurta phyjama by RG Designers. Made from Cotton Fabric , it would lend you a glamorous look that is just perfect for any Occassion. self textured design kurta with White churidaar bottom.</Description>
<BulletPoint>Made out of 100% Cotton fabric, Regular Fit, Full Sleeve, Chinese Collar Kurta For Men , Regular Fit white colored Pyjama with drawstring closure,</BulletPoint>
<MSRP currency="INR">25.19</MSRP>
<Manufacturer>Brand</Manufacturer>
<ItemType>Ethnicwear</ItemType>
</DescriptionData>
</Product>
</Message>
</AmazonEnvelope>
Kindly provide some help in fixing this issue.
Thanks.
Your XML is valid (except it has the final </Message></AmazonEnvelope> missing)
To add a product to Amazon which is not currently there, you will need to provide the ProductType structure. The following is a minimal valid XML containing one: only changes are after </DescriptionData>
<?xml version="1.0" encoding="iso-8859-1"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>A28XP9******</MerchantIdentifier>
</Header>
<MessageType>Product</MessageType>
<PurgeAndReplace>false</PurgeAndReplace>
<Message>
<MessageID>1</MessageID>
<OperationType>Update</OperationType>
<Product>
<SKU>SKU RGDSP*********</SKU>
<ProductTaxCode>A_GEN_NOTAX</ProductTaxCode>
<DescriptionData>
<Title>Full Sleeves Kurta Pyjama Set For Men</Title>
<Brand> Brand </Brand>
<Description>Look your ethnic best when you adorn this kurta phyjama by RG Designers. Made from Cotton Fabric , it would lend you a glamorous look that is just perfect for any Occassion. self textured design kurta with White churidaar bottom.</Description>
<BulletPoint>Made out of 100% Cotton fabric, Regular Fit, Full Sleeve, Chinese Collar Kurta For Men , Regular Fit white colored Pyjama with drawstring closure,</BulletPoint>
<MSRP currency="INR">25.19</MSRP>
<Manufacturer>Brand</Manufacturer>
<ItemType>Ethnicwear</ItemType>
</DescriptionData>
<ProductData>
<Clothing>
<ClassificationData>
<ClothingType>Sleepwear</ClothingType>
<Department>x</Department>
<StyleKeywords>x</StyleKeywords>
</ClassificationData>
</Clothing>
</ProductData>
</Product>
</Message>
</AmazonEnvelope>

Amazon mws - Update MerchantOrderID

I'm trying to update the Amazon Merchant Order ID through the Feed API.
It was succesfully submitted and I've checked the response from Amazon MWS Scratchpad by check with the GetFeedSubmissionResult call. it returned that the process was completed without any erro.
But when I open my order in Amazon (sellercentral.amazon.com) it says "none saved": Your Merchant Order ID: # none saved
Nothing have been changed.
Your XML feed (provided in your comment) is missing the <Item> elements. Amazon wants you to acknowledge that you have not only received the order, but also all contained items. A complete XML feed should look like this:
<?xml version="1.0"?>
<AmazonEnvelope xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>M_xxxxx_114513393</MerchantIdentifier>
</Header>
<MessageType>OrderAcknowledgement</MessageType>
<Message>
<MessageID>1</MessageID>
<OrderAcknowledgement>
<AmazonOrderID>114-8862878-1197857</AmazonOrderID>
<MerchantOrderID>abc-402637</MerchantOrderID>
<StatusCode>Success</StatusCode>
<Item> <-- you need to repeat the "Item" element for each order line
<AmazonOrderItemCode>abc</AmazonOrderItemCode>
<MerchantOrderItemID>def</MerchantOrderItemID> <-- I'm not sure this is required, but haven't tried without
</Item>
</OrderAcknowledgement>
</Message>
</AmazonEnvelope>
( the <-- ... comments are not part of the actual feed)
You may also wish to look at this related StackOverflow answer.

IBM Connections API - Adding Members to Wiki

I've been trying to add members to the wiki to no avail. Here's the link to the instructions on how to do so:
http://www-10.lotus.com/ldd/appdevwiki.nsf/xpDocViewer.xsp?lookupName=IBM+Connections+4.5+API+Documentation#action=openDocument&res_title=Updating_a_wiki_ic45&content=pdcontent
Basically, what I need to do is to retrieve the wiki first using this URL:
connectionsURL/wikis/basic/api/wiki/{wiki-label}/entry
And then append the information there and then send it back using a PUT request, Content-Type: application/atom+xml. The content passed should look like the one below based on the example:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<id>ignore</id>
<td:label xmlns:td="urn:ibm.com/td">Lake Champlain</td:label>
<category term="wiki" scheme="tag:ibm.com,2006:td/type" label="wiki">
</category>
<author>
<name>Bill Jones</name>
<snx:userid xmlns:snx="http://www.ibm.com/xmlns/prod/sn">
bf9117c0-f8f2-102c-97c4-ceb7f24c5708
</snx:userid>
<email>bjones#us.example.com</email>
</author>
<td:sharedWith xmlns:td="urn:ibm.com/td">
<ca:member
xmlns:ca="http://www.ibm.com/xmlns/prod/composite-applications/v1.0"
ca:id="new_user_id"
ca:type="user"
ca:role="editor" >
</ca:member>
<ca:member
xmlns:ca="http://www.ibm.com/xmlns/prod/composite-applications/v1.0"
ca:id="anonymous-user"
ca:type="virtual"
ca:role="reader" >
</ca:member>
</td:sharedWith>
<title type="text">Long Lake</title>
<summary type="text">
modification none
</summary>
</entry>
I tried that and I always keep getting this 400 Bad Request response:
<?xml version="1.0" encoding="UTF-8" ?>
<td:error>
<td:errorCode>InvalidRequest</td:errorCode>
<td:errorMessage>Atom entry is null</td:errorMessage>
</td:error>
Try this API - https://conServer.com/wikis/basic/api/wiki/{WIKI_LABEL}/members
Then do a PUT on that resource to change the entries (I think type should be atom/xml or atom+cat/xml
Thanks
paul

Tags with an underscore cause failure with BeautifulSoup selector

XML File:
<?xml version="1.0" encoding="UTF-8"?>
<sites>
<site>
<name>Default</name>
<url_namespace>default</url_namespace>
</site>
</sites>
Soup info:
soup = BeautifulSoup(xml)
soup.select('url_namespace')
Error:
ValueError: Unsupported or invalid CSS selector: "url_namespace"
How does one select an xml tag, or and id which contains an underscore?
I'd suggest lxml just because this could be done with a simple XPath, but just for the fun of showing how to select an invalid CSS selector... well, you actually don't. There are a couple of things that can be done, one of which is to replace the offensive tag with perhaps a div tag with a specific class, so you can select it.
However, one really hackish way of doing this really quickly is to just change the name property of each element you find.
from bs4 import BeautifulSoup as bsoup
data = """
<?xml version='1.0' encoding='UTF-8'?>
<sites>
<site>
<name>Default</name>
<url_namespace>default1</url_namespace>
<url_namespace>default2</url_namespace>
<url_namespace>default3</url_namespace>
<url_namespace>default4</url_namespace>
</site>
</sites>
"""
soup = bsoup(data)
elements = soup.find_all("url_namespace")
for element in elements:
element.name = "urlnamespace"
print soup
The above changes the soup to the following:
<html><body><sites>
<site>
<name>Default</name>
<urlnamespace>default1</urlnamespace>
<urlnamespace>default2</urlnamespace>
<urlnamespace>default3</urlnamespace>
<urlnamespace>default4</urlnamespace>
</site>
</sites>
</body></html>
Adding the following codeblock to the above code...
targets = soup.select("urlnamespace")
for target in targets:
print target.get_text()
... gives you the following result:
default1
default2
default3
default4
Not really the prettiest way, but it works. Out of sheer curiosity, though, why the need to select the tag this way? find_all works on the tag, as you can see above.
Anyway, let us know if this works.

creating relationship feeds through Amazon MWS

One of my client wants me to upload and maintain the variations of the products on Amazon. I am stuck in creating relationship(Variations) feed. I have googled it for many hours but with no luck.
I can easily generate and upload simple product, inventory, image and price feeds successfully through MWS, but my concept regarding variation feeds is not clear.
To simplify, my client has a product say "ABC" with two different colors (Black,Red). The ABC is not yet Registered on Amazon. The client has EAN for this product to list on amazon and also show the two different variations.
Now if any body can help me out as how to make the feeds to reflect the variations.
Please explain with practical code example.
Thanks
you can use following XML to create relationship.Use "_POST_PRODUCT_RELATIONSHIP_DATA_" as type of feed
<?xml version="1.0" encoding="utf-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>xxxxxxxxxxxxxxx</MerchantIdentifier>
</Header>
<MessageType>Relationship</MessageType>
<Message>
<MessageID>1</MessageID>
<OperationType>Update</OperationType>
<Relationship>
<ParentSKU>skt_parent1234</ParentSKU>
<Relation>
<SKU>skt_white123</SKU>
<Type>Variation</Type>
</Relation>
<Relation>
<SKU>skt_orange1234</SKU>
<Type>Variation</Type>
</Relation>
</Relationship>
</Message>
</AmazonEnvelope>