iT邦幫忙

0

PostMan 測試POST 問題請教

  • 分享至 

  • xImage

您好:
參考
https://coolmandiary.blogspot.com/2021/12/aspnet-webapi2003mvcwebapipostmanhttpve.html

POST可以過!

做到 PUT 修改更新特定某一筆
但我範本操作

http://localhost:54302/api/Products/2
RAW (JSON)內要修改
{
"Id": 2,
"ProductName": "Orange2",
"Price": 66.0
}

卻出現錯誤,請問這該如何除錯?

2.另外,網站範例,有VS中斷點,要如何POSTMAN測試時,觸發起該中斷點

3.若POST進去NULL,要如何清除?

4.資料作亂了,要如何回復原有資料

謝謝!

補充

<body>
	<div id="content">
		<div class="content-container">
			<h3>HTTP Error 404.0 - Not Found</h3>
			<h4>您尋找的資源已移除、名稱已變更,或暫時無法使用。</h4>
		</div>
		<div class="content-container">
			<fieldset>
				<h4>最有可能的原因:</h4>
				<ul>
					<li>網頁伺服器上不存在指定的目錄或檔案。</li>
					<li>URL 包含拼字錯誤。</li>
					<li>自訂篩選器或模組 (例如 URLScan) 限制存取此檔案。</li>
				</ul>
			</fieldset>
		</div>
		<div class="content-container">
			<fieldset>
				<h4>解決方法:</h4>
				<ul>
					<li>在 Web 服务器上创建内容。</li>
					<li>检查浏览器 URL。</li>
					<li>检查失败请求跟踪日志并查看哪个模块正在调用 SetStatus。有关详细信息,请单击<a
							href="http://go.microsoft.com/fwlink/?LinkID=66439">此处</a>。</li>
				</ul>
			</fieldset>
		</div>

		<div class="content-container">
			<fieldset>
				<h4>Detailed Error Information:</h4>
				<div id="details-left">
					<table border="0" cellpadding="0" cellspacing="0">
						<tr class="alt">
							<th>Module</th>
							<td>&nbsp;&nbsp;&nbsp;IIS Web Core</td>
						</tr>
						<tr>
							<th>Notification</th>
							<td>&nbsp;&nbsp;&nbsp;MapRequestHandler</td>
						</tr>
						<tr class="alt">
							<th>Handler</th>
							<td>&nbsp;&nbsp;&nbsp;StaticFile</td>
						</tr>
						<tr>
							<th>Error Code</th>
							<td>&nbsp;&nbsp;&nbsp;0x80070002</td>
						</tr>

					</table>
				</div>
				<div id="details-right">
					<table border="0" cellpadding="0" cellspacing="0">
						<tr class="alt">
							<th>Requested URL</th>
							<td>&nbsp;&nbsp;&nbsp;http://localhost:54302/api/Products/3</td>
						</tr>
						<tr>
							<th>Physical Path</th>
							<td>&nbsp;&nbsp;&nbsp;D:\_code\mvcapi\03\WebApi03\WebApi03_E\api\Products\3</td>
						</tr>
						<tr class="alt">
							<th>Logon Method</th>
							<td>&nbsp;&nbsp;&nbsp;匿名</td>
						</tr>
						<tr>
							<th>Logon User</th>
							<td>&nbsp;&nbsp;&nbsp;匿名</td>
						</tr>

					</table>
					<div class="clear"></div>
				</div>
			</fieldset>
		</div>

		<div class="content-container">
			<fieldset>
				<h4>More Information:</h4>
				此錯誤表示伺服器上不存在此檔案或目錄。請建立檔案或目錄,然後再次嘗試要求。
				<p><a href="https://go.microsoft.com/fwlink/?LinkID=62293&amp;IIS70Error=404,0,0x80070002,19044">View
						more information &raquo;</a></p>

			</fieldset>
		</div>
	</div>
</body>

</html>

程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi03_E.Models;

namespace WebApi03_E.Controllers
{
    public class ProductsController : ApiController
    {
        static List<Product> products = new List<Product>()
        {
            new Product(){Id = 0,ProductName = "Apple",Price = 30},
            new Product(){Id = 1,ProductName = "Banana",Price = 40},
            new Product(){Id = 2,ProductName = "Orange",Price = 60}
        };

        // GET: api/Products
        [HttpGet]
        public IHttpActionResult LoadProducts()
        {
            return Ok(products);
        }

        // GET: api/Products/5
        [HttpGet]
        public Product GetSpecificProduct(int id)
        {
            return products[id];
        }

        // POST: api/Products
        [HttpPost]
        public HttpResponseMessage Post([FromBody] Product product)
        {
            products.Add(product);
            return new HttpResponseMessage(HttpStatusCode.Created);
        }

        // PUT: api/Products/5
        [HttpPut]
        public void Put([FromUri] int id, [FromBody] Product product)
        {
            products[id] = product;
        }

        // DELETE: api/Products/5
        [HttpDelete]
        public void Delete(int id)
        {
            products.RemoveAt(id);
        }

    }
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
天黑
iT邦研究生 5 級 ‧ 2022-03-04 08:49:03
最佳解答
  1. 目前你貼的錯誤訊息是 http://localhost:54302/api/Products/3
    並不是你敘述的輸入內容,是不是要確認一下,根據你提供的網站內容預設只有0-2的id可提供修改
						<tr class="alt">
							<th>Requested URL</th>
					<td>&nbsp;&nbsp;&nbsp;http://localhost:54302/api/Products/3</td>
						</tr>
  1. 中斷點的使用,有蠻多文章的幫你找了一篇

  2. POST進去NULL,要如何清除
    上面有Delete的API可以帶入對應Id刪除

  3. 資料作亂了,要如何回復原有資料
    IIS Express停止再啟動就可以了,網頁上的範例是靜態資料,每次都會回復

看更多先前的回應...收起先前的回應...
noway iT邦研究生 2 級 ‧ 2022-03-04 19:21:58 檢舉

您好:
1.我測試2 或 3,裡面有都資料; 3是新增的。
目前 UPDATE 與DELTE 都無法運作

3.之前看過

{
{
"Id": 2,
"ProductName": "Orange2",
"Price": 66.0
}
,
null
}

這個NULL,可以刪嗎?

noway iT邦研究生 2 級 ‧ 2022-03-05 10:38:05 檢舉

您好:
我發現

public HttpResponseMessage Post([FromBody] Product product)

public IHttpActionResult LoadProducts()

皆可 觸法 debug中斷點

    [HttpPut]
        public void Put([FromUri] int id, [FromBody] Product product)

卻沒有進去中斷點,是否 這段 哪一點 有問題?
謝謝!

天黑 iT邦研究生 5 級 ‧ 2022-03-07 11:01:31 檢舉
  1. null部分,新增是異常的資料沒有id無法刪除,建議新增或者修改前驗證資料型態是否正常
  2. PUT沒有進入中斷點,請提供呼叫的方式喔,連結跟內容,沒有進去中斷就是沒有呼叫到..
noway iT邦研究生 2 級 ‧ 2022-03-07 19:26:27 檢舉

您好:
目前SERVER端程式如下

 // PUT: api/Products/5
        [HttpPut]
        public void Put([FromUri] int id, [FromBody] Product product)
        {
            products[id] = product;
        }

GET ---OK
https://ithelp.ithome.com.tw/upload/images/20220307/20104095uqEoO15MsL.png

--PUT--ERR
https://ithelp.ithome.com.tw/upload/images/20220307/20104095DNj5yxb6ip.png

<body>
	<div id="content">
		<div class="content-container">
			<h3>HTTP Error 404.0 - Not Found</h3>
			<h4>您尋找的資源已移除、名稱已變更,或暫時無法使用。</h4>
		</div>
		<div class="content-container">
			<fieldset>
				<h4>最有可能的原因:</h4>
				<ul>
					<li>網頁伺服器上不存在指定的目錄或檔案。</li>
					<li>URL 包含拼字錯誤。</li>
					<li>自訂篩選器或模組 (例如 URLScan) 限制存取此檔案。</li>
				</ul>
			</fieldset>
		</div>
		<div class="content-container">
			<fieldset>
				<h4>解決方法:</h4>
				<ul>
					<li>在 Web 服务器上创建内容。</li>
					<li>检查浏览器 URL。</li>
					<li>检查失败请求跟踪日志并查看哪个模块正在调用 SetStatus。有关详细信息,请单击<a
							href="http://go.microsoft.com/fwlink/?LinkID=66439">此处</a>。</li>
				</ul>
			</fieldset>
		</div>

		<div class="content-container">
			<fieldset>
				<h4>Detailed Error Information:</h4>
				<div id="details-left">
					<table border="0" cellpadding="0" cellspacing="0">
						<tr class="alt">
							<th>Module</th>
							<td>&nbsp;&nbsp;&nbsp;IIS Web Core</td>
						</tr>
						<tr>
							<th>Notification</th>
							<td>&nbsp;&nbsp;&nbsp;MapRequestHandler</td>
						</tr>
						<tr class="alt">
							<th>Handler</th>
							<td>&nbsp;&nbsp;&nbsp;StaticFile</td>
						</tr>
						<tr>
							<th>Error Code</th>
							<td>&nbsp;&nbsp;&nbsp;0x80070002</td>
						</tr>

					</table>
				</div>
				<div id="details-right">
					<table border="0" cellpadding="0" cellspacing="0">
						<tr class="alt">
							<th>Requested URL</th>
							<td>&nbsp;&nbsp;&nbsp;http://localhost:54302/api/products/2</td>
						</tr>
						<tr>
							<th>Physical Path</th>
							<td>&nbsp;&nbsp;&nbsp;D:\_code\mvcapi\03\WebApi03\WebApi03_E\api\products\2</td>
						</tr>
						<tr class="alt">
							<th>Logon Method</th>
							<td>&nbsp;&nbsp;&nbsp;匿名</td>
						</tr>
						<tr>
							<th>Logon User</th>
							<td>&nbsp;&nbsp;&nbsp;匿名</td>
						</tr>

					</table>
					<div class="clear"></div>
				</div>
			</fieldset>
		</div>

		<div class="content-container">
			<fieldset>
				<h4>More Information:</h4>
				此錯誤表示伺服器上不存在此檔案或目錄。請建立檔案或目錄,然後再次嘗試要求。
				<p><a href="https://go.microsoft.com/fwlink/?LinkID=62293&amp;IIS70Error=404,0,0x80070002,19044">View
						more information &raquo;</a></p>

			</fieldset>
		</div>
	</div>
</body>
天黑 iT邦研究生 5 級 ‧ 2022-03-08 10:56:01 檢舉

恩....我發現跟程式沒關係
POSTMAN要輸入的地方,是上面的部分下面是回傳的

https://ithelp.ithome.com.tw/upload/images/20220308/20138525x2wJEQOvq6.png

noway iT邦研究生 2 級 ‧ 2022-03-08 19:27:30 檢舉

您好:沒錯我是在上面區塊輸入(POST 就可成功)

https://ithelp.ithome.com.tw/upload/images/20220308/201040955S4waLlZMg.png

天黑 iT邦研究生 5 級 ‧ 2022-03-09 09:52:22 檢舉

如果有解答你的疑問 再麻煩你幫我標為解答 謝謝

noway iT邦研究生 2 級 ‧ 2022-03-09 20:45:48 檢舉

但 我PUT不能成功 .....
您方便提供 程式碼 嗎? 謝謝!

天黑 iT邦研究生 5 級 ‧ 2022-03-10 12:01:54 檢舉

太神了... 程式我沒有改動過... PUT應該用上面的方式就可以過了

如下圖

https://ithelp.ithome.com.tw/upload/images/20220310/20138525uBM7ZwxYYm.png

noway iT邦研究生 2 級 ‧ 2022-03-10 22:25:34 檢舉

您建置專案 是用HTTP 還是 HTTPS

天黑 iT邦研究生 5 級 ‧ 2022-03-11 10:50:47 檢舉

HTTP,我看你POSTMAN設定的,POST能過PUT應該也要正常,確認一下有沒有差異吧

連結: http://localhost:8001/api/Products/2

{
    "Id" : 2,
    "ProductName" : "Big Orange",
    "Price" : 70.0
}

model

    public class Product
    {
        public int Id { get; set; }
        public string ProductName { get; set; }
        public decimal Price { get; set; }
    }

code

    public class ProductsController : ApiController
    {
        static List<Product> products = new List<Product>()
        {
            new Product(){Id = 0,ProductName = "Apple",Price = 30},
            new Product(){Id = 1,ProductName = "Banana",Price = 40},
            new Product(){Id = 2,ProductName = "Orange",Price = 60}
        };

        // GET: api/Products
        [HttpGet]
        public IHttpActionResult LoadProducts()
        {
            return Ok(products);
        }

        // GET: api/Products/5
        [HttpGet]
        public Product GetSpecificProduct(int id)
        {
            return products[id];
        }

        // POST: api/Products
        [HttpPost]
        public HttpResponseMessage Post([FromBody] Product product)
        {
            products.Add(product);
            return new HttpResponseMessage(HttpStatusCode.Created);
        }

        // PUT: api/Products/5
        [HttpPut]
        public void Put([FromUri] int id, [FromBody] Product product)
        {
            products[id] = product;
        }

        // DELETE: api/Products/5
        [HttpDelete]
        public void Delete(int id)
        {
            products.RemoveAt(id);
        }
    }
noway iT邦研究生 2 級 ‧ 2022-03-17 21:15:54 檢舉

您好: 我從新建一個專案,並使用系統範本, 且不用HTTPS
可以過。
但一個問題
目前ID有 0,1,2
我新增一筆ID=4,
再新增一筆ID=3

{
    "Id": 4,
    "ProductName": "ZZ",
    "Price": 72.0
}

{
    "Id": 3,
    "ProductName": "33",
    "Price": 12.0
}

我去http://localhost:49788/api/Products/3

{
"Id": 3,
"ProductName": "AA33",
"Price": 92.0
}

但他一異動的是 索引第3筆
{
"Id": 4,
"ProductName": "ZZ",
"Price": 72.0
}
那 椅面的Id 並不是KEY?

天黑 iT邦研究生 5 級 ‧ 2022-03-18 09:21:27 檢舉
static List<Product> products = new List<Product>()
        {
            new Product(){Id = 0,ProductName = "Apple",Price = 30},
            new Product(){Id = 1,ProductName = "Banana",Price = 40},
            new Product(){Id = 2,ProductName = "Orange",Price = 60}
        };

[HttpPut]
        public void Put([FromUri] int id, [FromBody] Product product)
        {
            products[id] = product;
        }

範例這裡修改是List的index並非資料的id喔,如果想依照id調整,程式部分需要調整,整組程式都是圍繞著list的index在座處理的

我要發表回答

立即登入回答