空手道API:使用PUT方法发送请求时,出现“不允许405方法”错误

时间:2020-08-17 04:10:01

标签: postman karate

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

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

任何人都可以指导我解决此问题吗?

注意:

  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
    }
  }
}'

1 个答案:

答案 0 :(得分:1)

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

library(stringdist)

a = c('foo', 'bar', 'baz', 'li')
b = c('foo', 'food', 'fo', 'fod')

a = as.data.frame(t(combn(a, 2)))
b= as.data.frame(t(combn(b, 2)))

distances = c()

for (i in 1:nrow(a)){
 cos_dist = stringdist(a$V1[i], a$V2[i], method="cosine")
 distances = c(distances, cos_dist)
}

print(mean(a_distances))
[1] 0.8888889

distances = c()

for (i in 1:nrow(b)){
  cos_dist = stringdist(b$V1[i], b$V2[i], method="cosine")
  distances = c(distances, cos_dist)
}

print(mean(b_distances))
[1] 0.1230863

其他疑难解答提示:

  • 邮递员会自动添加一些标题,例如* 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标头中,在极少数情况下服务器不喜欢(很可能是服务器端的错误)-您可以通过{{1 }}-请参见https://stackoverflow.com/a/53651454/143475