简体   繁体   中英

Invoke-RestMethod - POST response format not JSON?

If I use methods like PUT, DELETE or GET I don't have any problem. $wresponse is normal JSON reponse.

When I try to use POST I get response that looks like HTML.

Execution code:

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

After execution product is normally added to database but response looks like this (JSON after del tag?):

    <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"}]}}    

Where could be the problem? Or is this normal and I should somehow extract JSON from this response?

This is called POST/REDIRECT/GET . Websites do this to prevent duplicate submissions from a web browser. This is a server-side implementation issue, not an issue with your PowerShell (unless the API documentation mentions this redirect, in which case you need to have PowerShell handle the redirect and follow it).

Do you have access to the backend code running this API? I think that's where you need to look.

Looks to me like a 302 Object Moved response. You get HTML first and then JSON response appended at the end.

Notice that the HTML containing the tag is actually a json escaped string literal value, it's the value of "price_html". That's why all the double quotes are escaped.

Anyway look for your problem in the backend code, for some reason it's returning a combination of HTML code first and your (presumeably expected) JSON response appended after at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM