Invoke-RestMethod-POST响应格式不是JSON吗?

时间:2019-02-04 18:15:16

标签: json powershell post response

如果我使用PUT,DELETE或GET之类的方法,我没有任何问题。 $wresponse是正常的JSON响应。

当我尝试使用POST时,我得到的响应看起来像HTML。

执行代码:

$wresponse = Invoke-RestMethod -Uri https://**************/wp-json/wc/v3/products/ -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body -ContentType "application/json" -Method post

通常将执行后产品添加到数据库中,但响应如下所示(del标签后为JSON?):

    <head>
    <title>Document Moved</title>
</head>
<body>
    <h1>Object Moved</h1>This document may be found
    <a HREF="https://*************/wp-json/wc/v3/products/1507">here</a>
</body> {"id":1507,"name":"test article","slug":"test-article","permalink":"https:\/\/************\/izdelek\/testni-artikel\/","date_created":"2019-01-29T11:29:15","date_created_gmt":"2019-01-29T10:29:15","date_modified":"2019-01-29T11:29:15","date_modified_gmt":"2019-01-29T10:29:15","type":"simple","status":"publish","featured":false,"catalog_visibility":"visible","description":"bla bla bla","short_description":"bla bla bla","sku":"","price":"10","regular_price":"20","sale_price":"10","date_on_sale_from":null,"date_on_sale_from_gmt":null,"date_on_sale_to":null,"date_on_sale_to_gmt":null,"price_html":"
<del>
    <span class=\"woocommerce-Price-amount amount\">
        <span class=\"woocommerce-Price-currencySymbol\">&euro;<\/span>20.00<\/span><\/del>
            <ins>
                <span class=\"woocommerce-Price-amount amount\">
                    <span class=\"woocommerce-Price-currencySymbol\">&euro;<\/span>10.00<\/span><\/ins>","on_sale":true,"purchasable":true,"total_sales":0,"virtual":false,"downloadable":false,"downloads":[],"download_limit":-1,"download_expiry":-1,"external_url":"","button_text":"","tax_status":"taxable","tax_class":"","manage_stock":false,"stock_quantity":null,"stock_status":"instock","backorders":"no","backorders_allowed":false,"backordered":false,"sold_individually":false,"weight":"812","dimensions":{"length":"100","width":"200","height":"300"},"shipping_required":true,"shipping_taxable":true,"shipping_class":"","shipping_class_id":0,"reviews_allowed":true,"average_rating":"0","rating_count":0,"related_ids":[298,342,229,224,32],"upsell_ids":[],"cross_sell_ids":[],"parent_id":0,"purchase_note":"","categories":[{"id":17,"name":"BABY LINE","slug":"baby-line"}],"tags":[],"images":[],"attributes":[],"default_attributes":[],"variations":[],"grouped_products":[],"menu_order":0,"meta_data":[],"_links":{"self":[{"href":"https:\/\/***************\/wp-json\/wc\/v3\/products\/1507"}],"collection":[{"href":"https:\/\/*******************\/wp-json\/wc\/v3\/products"}]}}    

问题可能出在哪里?还是这很正常,我应该以某种方式从此响应中提取JSON?

2 个答案:

答案 0 :(得分:1)

这称为POST/REDIRECT/GET。网站这样做是为了防止来自Web浏览器的重复提交。这是服务器端的实现问题,而不是PowerShell的问题(除非API文档提到此重定向,在这种情况下,您需要让PowerShell处理重定向并遵循它)。

答案 1 :(得分:1)

您有权访问运行此API的后端代码吗?我认为那是您需要看的地方。

在我看来就像302 Object Moved响应。您首先获得HTML,然后在末尾附加JSON响应。

请注意,包含标记的HTML实际上是一个json转义的字符串文字值,它是“ price_html”的值。这就是为什么所有双引号都被转义的原因。

无论如何都在后端代码中寻找问题,由于某种原因,它首先返回HTML代码,然后返回您(可能是预期的)JSON响应的组合。