2012-02-07 11 views
1

Prestashop apiからXmlデータを取得できましたが、PUT/POST XmlデータをPrestaShop APIに送信できませんでした。C#REST APIクライアントPrestashop

誰かが私が間違っているかもしれないと示唆できますか?

public POST_xml() 
{ 
    Uri address = new Uri("http://.../api/countries/1"); 
    HttpWebRequest request = WebRequest.Create("http://.../api/countries/1") as HttpWebRequest; 

    NetworkCredential("15PJQ4V8CXI22JVW1TKZASDF0OAYNBLA", ""); 

    // Create the web request 
    request = WebRequest.Create(address) as HttpWebRequest; 

    // Set type to POST 
    request.Method = "POST"; 
    request.ContentType = "application/x-www-form-urlencoded"; 

    // Create the data we want to send. 

    string context = "<prestashop><country><id>1</id><id_zone xlink:href=\"http://.../api/zones/1\">"; 
    context += "1</id_zone><id_currency/><iso_code>DE</iso_code><call_prefix>49</call_prefix><active>1</active><contains_states>0</contains_states><need_identification_number>0</need_identification_number><need_zip_code>1</need_zip_code><zip_code_format>NNNNN</zip_code_format><display_tax_label>1</display_tax_label><name><language"; 
    context += " id=\"6\" xlink:href=\"http://.../api/languages/6\">Germanyxx</language></name></country></prestashop>"; 


    StringBuilder data = new StringBuilder(); 
    data.Append("&context=" + HttpUtility.UrlEncode(context)); 


    // Create a byte array of the data we want to send 
    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); 

    // Set the content length in the request headers 
    request.ContentLength = byteData.Length; 

    // Write data 
    using (Stream postStream = request.GetRequestStream()) 
    { 
     postStream.Write(byteData, 0, byteData.Length); 
    } 
} 

答えて

1
  1. PrestaShopののWebサービスAPIは、XMLは、POSTリクエストのためのxml =を前置することを期待しています。
  2. Fiddler - Http Debuggingプロキシと呼ばれるプログラムを使用して、送信している内容とWebサービスが返信している内容を正確に確認します。
1
  1. あなたは、ID = 1で国を更新したい場合は、
  2. それはまだ動作しない場合は「&文脈」
  3. を追加する必要はありませ「を入れないで」する要求タイプを設定することになるでしょう。サンプルコードはhttp://doc.prestashop.com/display/PS14/Using+the+REST+webserviceにダウンロードしてください。次に、コード "update.php"を実行して、リクエストをどのように作成するかを確認します。
2

私はC#でエクスポートしていませんが、JavaとVBAの両方でREST呼び出しを作成しました。あなたのコードに間違いはありません。 新しい国のエントリを作成したいと思っています。 次に、あなたのURLアドレスが

Uri address = new Uri("http://.../api/countries/"); 

する必要があり、あなたの文字列のコンテキストで、新しいリソースのためのIDを設定することはできません。

ここに表示されているのは、リソース「countries/1」のPUTリクエストではほとんど機能します。