簡體   English   中英

POST請求到REST服務器

[英]POST request to a REST server

因此,我正在嘗試創建一個簡單的(我的意思是簡單的)POST請求。 這是服務器端的類。

@Stateless
@Path("cards")
public class CardsFacadeREST extends AbstractFacade<Cards> {

    @POST
    @Path("test")
    @Consumes({"text/plain"})
    public void createTestCard() {
        Cards card = new Cards();
        card.setName("Test Card");

        super.create(card);
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }
}

GET方法工作得很好,但是POST方法對我不起作用。 我正在使用Chrome的Advanced Rest Client。

就是這樣。

我不斷收到“ 400:錯誤的請求。客戶端發送的請求在語法上不正確。”

當我在JSON窗口中打開響應時,顯示的全部是“意外令牌<”

如果有什么不同,這里是請求標頭。

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: text/plain
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: JSESSIONID=f4c746a32b46244d422800192f04; treeForm_tree-    hi=treeForm:tree:applications

Body is empty.

以及響應:

X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.0 Java/Oracle Corporation/1.7) 
Server: GlassFish Server Open Source Edition 4.0 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Methods: GET, POST, PUT, DELETE 
Allow: GET,DELETE,OPTIONS,PUT,POST 
Access-Control-Allow-Headers: content-type 
Content-Language:  
Content-Type: text/html Date: Thu, 11 Feb 2016 20:48:12 GMT 
Connection: close Content-Length: 1105

Body:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>GlassFish Server Open Source Edition  4.0  - Error report</title><style type="text/css"><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - Bad Request</h1><hr/><p><b>type</b> Status report</p><p><b>message</b>Bad Request</p><p><b>description</b>The request sent by the client was syntactically incorrect.</p><hr/><h3>GlassFish Server Open Source Edition  4.0 </h3></body></html>

400錯誤的請求響應很可能是因為服務器已表示期望使用特定的內容類型

@Consumes({"text/plain"}

但是,客戶端未指示該帖子主體是這種類型的。

要解決此問題,請確保來自客戶端的POST請求包含以下HTTP標頭:

內容類型:文本/純文本

或者,可能是您沒有發布純文本而打算發布XML或JSON的情況。 無論使用哪種預期類型,您只需要確保客戶端和服務器就此達成一致即可。

如果HTTP請求具有主體,則它必須具有Content-Length或Transfer-Encoding標頭。

如果沒有,則請求沒有主體,甚至沒有長度為0的主體。如果要發送空主體,則請求應具有標頭Content-Length: 0

沒有主體和沒有主體之間存在語義上的區別。 服務器顯然拒絕了沒有正文的POST請求。 (盡管根據RFC,該請求實際上在語法上是有效的)


實際上,這部分內容還不太清楚( 討論主題 (請勿閱讀))。 某些實現將GET請求的Content-Length: 0Content-Length: 0 一些實現省略了Content-Length: 0表示空的POST正文; 兩者都是錯誤的...有時它們起作用,有時卻不起作用。 歡迎來到混亂的HTTP世界。

發生這種情況時,我會討厭。 完全是另外一回事,我的持久性bean被弄亂了。 當我在POST方法中注釋掉“ super.create(card)”時,一切正常(請求正文或否)。

仍然不知道為什么會導致“ 400:錯誤的請求。客戶端發送的請求在語法上不正確。”

似乎我還有其他問題要解決,但這至少可以解決。

謝謝大家的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM