【问题标题】:How to send this PUT-request to prestashop?如何将此 PUT 请求发送到 prestashop?
【发布时间】:2016-01-17 02:31:17
【问题描述】:

我正在使用Prestashop 网络服务。我正在尝试向 API 发送 PUT(更新)请求,但没有成功。我的请求似乎以“错误”的方式设置(即不是服务器期望的方式)因为 Prestashop 是开源的,所以我查看了源代码,特别是当它收到 PUT 请求时它会以下(我不写php代码):

$input_xml = null;
// if a XML is in PUT or in POST
if (($_SERVER['REQUEST_METHOD'] == 'PUT') || ($_SERVER['REQUEST_METHOD'] == 'POST')) {
    $putresource = fopen("php://input", "r");
    while ($putData = fread($putresource, 1024)) {
        $input_xml .= $putData;
    }
fclose($putresource);
  }
if (isset($input_xml) && strncmp($input_xml, 'xml=', 4) == 0) {
    $input_xml = substr($input_xml, 4);
}

从上面的代码中我了解到我的数据应该是这样的:xml=<data><here></here></data> 但我不知道该放在哪里,它应该在请求正文中还是嵌入在 url 中?当您使用Content-Type = text/xml 发送请求时,“xml=”是否隐含?我确实尝试了请求的不同组合,但仍然得到相同的404 错误。我试过这个:

let updateOrderState (orderId:int64) (stateId:int64) (credentials:AuthInfo) = 
    // url looks like this: http://www.webstoreexample.com/entity/id
    let auth = BasicAuth credentials.Key ""
    let orderApi = credentials.Api + "/orders/" + orderId.ToString();
    let orderAsXml = Http.RequestString(orderApi, httpMethod = "GET", headers = [auth])
    let xml = Order.Parse(orderAsXml).XElement // at this point, I have the data
    xml.Element(XName.Get("order")).Element(XName.Get("current_state")).SetValue(stateId) // field 'current_state' gets modified
    let xmlData = xml.ToString()
    // HERE the put request
    Http.RequestString(url = credentials.Api + "/orders", 
                       headers = [ auth; 
                                   "Content-Type","text/xml" ],         
                       httpMethod= HttpMethod.Put,
                       body= HttpRequestBody.TextRequest(xmlData))

PUT 请求的变体效果不佳,这里我将请求正文从 TextRequest 更改为 FormValues

Http.RequestString(url = credentials.Api + "/orders", 
                   headers = [ auth; 
                               "Content-Type","text/xml" ],         
                   httpMethod= HttpMethod.Put,
                   body= HttpRequestBody.FormValues ["xml", xmlData]) // xml=xmlData

我尝试的另一件事是将id 添加到网址(即使在文档中他们说这不是必需的):

Http.RequestString(url = credentials.Api + "/order/" + orderId.ToString(), // added the id to the url
                   headers = [ auth; 
                               "Content-Type","text/xml" ],         
                   httpMethod= HttpMethod.Put,
                   body= HttpRequestBody.FormValues ["xml", xmlData]) // xml=xmlData

具体来说,我正在尝试更新订单的current_state 节点的值。获取数据并对其进行修改按预期工作,但发送修改后的数据似乎不起作用,我仍然收到404: Not found 错误

对此的任何帮助将不胜感激!

【问题讨论】:

  • 不幸的是我不熟悉 F#,但也许 docs doc.prestashop.com/display/PS16/… 和 lib(尤其是 edit() 方法)github.com/PrestaShop/PrestaShop-webservice-lib/blob/master/… 会帮助你
  • 您有其他可用语言的示例代码吗?
  • @SergiiP,感谢您的回答,edit() 方法在这里没有多大帮助,因为它是从其他地方调用的(我不知道具体从哪里调用)。我已经看到了他们给出的更新“示例”,不幸的是,它仅在您使用服务器本身的 API 时才有效,因为他们在文件中实例化 PrestashopWebservice 类并发出一些 html。
  • 您是否有一个应用程序或 PHP 示例或其他可以让您至少在实验室条件下工作的东西?如果是这样,您能否将这两个请求都通过 Fiddler 并查看它们有何不同?这将使您了解问题所在。

标签: php xml rest f# prestashop


【解决方案1】:

好的,我刚刚使用我在 cmets 中提供的 libraryexample 对其进行了测试,并且我使用 CURL 重复相同的请求并获得相同的肯定结果,因此没有特定于 PHP 语言的内容。 我认为您只需要在应用程序中重复相同的标题/正文即可。

HTTP REQUEST HEADER
PUT /16011/api/orders/8 HTTP/1.1
Authorization: Basic TlpCUEJKTkhaWFpFMzlCMVBDTkdTM1JQN0s2NTVVQ0Y6
Host: localhost
Accept: */*
Content-Length: 2411
Content-Type: application/x-www-form-urlencoded

XML SENT
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<order>
    <id>8</id>
    <id_address_delivery xlink:href="http://localhost/16011/api/addresses/5">5</id_address_delivery>
    <id_address_invoice xlink:href="http://localhost/16011/api/addresses/5">5</id_address_invoice>
    <id_cart xlink:href="http://localhost/16011/api/carts/8">8</id_cart>
    <id_currency xlink:href="http://localhost/16011/api/currencies/1">1</id_currency>
    <id_lang xlink:href="http://localhost/16011/api/languages/1">1</id_lang>
    <id_customer xlink:href="http://localhost/16011/api/customers/2">2</id_customer>
    <id_carrier xlink:href="http://localhost/16011/api/carriers/3">3</id_carrier>
    <current_state xlink:href="http://localhost/16011/api/order_states/2" notFilterable="true">10</current_state>
    <module>bankwire</module>
    <invoice_number>0</invoice_number>
    <invoice_date>0000-00-00 00:00:00</invoice_date>
    <delivery_number>0</delivery_number>
    <delivery_date>0000-00-00 00:00:00</delivery_date>
    <valid>0</valid>
    <date_add>2015-09-17 08:29:17</date_add>
    <date_upd>2015-10-20 03:45:13</date_upd>
    <shipping_number notFilterable="true"></shipping_number>
    <id_shop_group>1</id_shop_group>
    <id_shop>1</id_shop>
    <secure_key>45838497c9182b0d361473894092de02</secure_key>
    <payment>Bank wire</payment>
    <recyclable>0</recyclable>
    <gift>0</gift>
    <gift_message></gift_message>
    <mobile_theme>0</mobile_theme>
    <total_discounts>0.000000</total_discounts>
    <total_discounts_tax_incl>0.000000</total_discounts_tax_incl>
    <total_discounts_tax_excl>0.000000</total_discounts_tax_excl>
    <total_paid>24.450000</total_paid>
    <total_paid_tax_incl>24.450000</total_paid_tax_incl>
    <total_paid_tax_excl>23.510000</total_paid_tax_excl>
    <total_paid_real>0.000000</total_paid_real>
    <total_products>16.510000</total_products>
    <total_products_wt>17.170000</total_products_wt>
    <total_shipping>7.280000</total_shipping>
    <total_shipping_tax_incl>7.280000</total_shipping_tax_incl>
    <total_shipping_tax_excl>7.000000</total_shipping_tax_excl>
    <carrier_tax_rate>4.000</carrier_tax_rate>
    <total_wrapping>0.000000</total_wrapping>
    <total_wrapping_tax_incl>0.000000</total_wrapping_tax_incl>
    <total_wrapping_tax_excl>0.000000</total_wrapping_tax_excl>
    <round_mode>2</round_mode>
    <conversion_rate>1.000000</conversion_rate>
    <reference>ECHCBFWGR</reference>
<associations></associations>
</order>
</prestashop>

【讨论】:

  • 感谢您的回答,我复制了相同的标题,(见这里)jsfiddle.net/w51re4bf,我仍然得到404 Error
  • 现在我尝试了 Php 网络服务,我能够发送 GET 请求并且按预期工作,PUT 请求发送成功,但这次我得到了一个不同的错误,即“Bad ID”,请看看这个jsfiddle.net/rbq5jza5/2
  • 并且您拥有此 api 密钥的编辑权限?等
  • 是的,我拥有所有权限,否则我应该得到403: Unauthorized 而不是404
  • 我只是尝试了任何想法,就我而言,这一切都不适用于您。我只能建议你一个帮助,给我临时密钥,我会检查你的安装
猜你喜欢
  • 2011-06-13
  • 2019-12-12
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 2012-07-09
  • 2011-01-10
  • 2021-09-05
相关资源
最近更新 更多