空手道API:获取"405方法不允许"使用PUT方法发送请求时出错 [英] Karate API: Getting "405 Method Not Allowed" Error when sending request with PUT method

查看:54
本文介绍了空手道API:获取"405方法不允许"使用PUT方法发送请求时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PUT方法发送请求时,出现以下错误.

I am getting the error below when sending a request with PUT method.

1 < 405
1 < Allow: GET,HEAD,POST,OPTIONS
1 < Content-Type: text/html; charset=iso-8859-1
1 < Date: Mon, 17 Aug 2020 04:01:07 GMT
1 < Server: Apache
1 < Vary: Accept-Encoding
1 < Via: 1.1 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>405 Method Not Allowed</title>
</head><body>
<h1>Method Not Allowed</h1>
<p>The requested method PUT is not allowed for the URL /xyz/abc/def/-1.</p>
</body></html>

[Fatal Error] :1:50: White spaces are required between publicId and systemId.
13:01:07.437 [main] WARN  com.intuit.karate - xml parsing failed, response data type set to string: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 50; White spaces are required between publicId and systemId.

我的请求正文如下:

{
  "i": {
    "x1": {
      "q": 10
    },
    "x2": {
      "q": 50
    }
  }
}

有人可以指导我解决问题吗?

Can anyone guide me to resolve the issue?

注意:

  1. API支持PUT方法,我们尝试使用的请求就是PUT.
  2. 相同的请求对于Postman也可以正常工作 .

发出请求的我的代码

 Scenario: Common Action
       * string testdata = read(sourceFilename)
       * def arr = LIB.getJsonArrayForElement(testdata, 'testCases')
       * eval logTC(arr[cnt].caseId)       
       #setting c
       * def c = arr[cnt].request.c == null ? 'Y' : arr[cnt].request.c
       #setting b
       * def b = arr[cnt].request.b == null ? 'Z' : arr[cnt].request.b
       #setting s
       * def s = arr[cnt].request.s == null ? '' : arr[cnt].request.s
       #setting i
       * def i = arr[cnt].request.i == null ? '' : arr[cnt].request.i
       #setting d
       * def d = arr[cnt].request.d == null ? false : arr[cnt].request.d
       #setting v
       * def v = arr[cnt].request.v == null ? '' : arr[cnt].request.v
       #setting f
       * def f = arr[cnt].request.f == null ? false : arr[cnt].request.f
       #setting es
       * def es = arr[cnt].expected.s == null ? 999999 : arr[cnt].expected.s
       * eval if(es == 999999 && typeof ess != 'undefined') karate.set('es', ess)
       * eval if(es == 999999) karate.set('es', 200)
       #preparing endpoint              
       * def endpoint = utils.getUrl(c, b, s, i, v, d)              
       
   Given url endpoint
     And request arr[cnt].request.body
    When method PUT 
    Then assert responses == es
       # for the cases where we are expecting 404
       * if (es == 404) karate.abort() 
       # for cases != 404
       * def expRes = '<empty>' 
       * eval if(es != 204) karate.set('expRes', arr[cnt].expected.body)
       * def actRes = '<empty>'
       * eval if(es != 204) karate.set('actRes', response)
     And match actRes == expRes

cURL

curl --location --request PUT 'http://host-url/c/Y/b/X/s/2/i/1/v/-1' \
--header 'X-Client-Id: test' \
--header 'Content-Type: application/json' \
--data-raw '{
  "i": {
    "x1": {
      "q": 10
    },
    "x2": {
      "q": 50
    }
  }
}'

推荐答案

此请求对我而言绝对有效,因此您的服务器很可能在期待其他内容,或者这可能是服务器中的实际错误.请与拥有服务器的团队合作,您应该能够立即找到问题.

This request works absolutely fine for me, so most likely your server was expecting something else, or maybe it is an actual bug in your server. Please work with the team who owns the server, you should be able to find the issue in no time.

* url 'http://httpbin.org'
* header X-Client-Id = 'test'
* path 'put'
* request
"""
{
  "i": {
    "x1": {
      "q": 10
    },
    "x2": {
      "q": 50
    }
  }
}
"""
* method put

其他疑难解答提示:

  • 邮递员会自动添加一些标题,例如Accept注意并在需要时添加
  • 空手道默认情况下将; charset=UTF-8附加到Content-Type标头,在极少数情况下服务器不喜欢(很可能是服务器端的错误)-您可以通过* configure charset = null禁用它-请参见 https://stackoverflow.com/a/53651454/143475
  • Postman adds some headers automatically such as Accept watch out for that and add that if needed
  • Karate appends ; charset=UTF-8 to the Content-Type header by default, which in rare cases the server does not like (most likely a bug on your server-side) - you can disable this by * configure charset = null - see https://stackoverflow.com/a/53651454/143475

这篇关于空手道API:获取"405方法不允许"使用PUT方法发送请求时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆