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

查看:11
本文介绍了空手道 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.

我的请求正文如下所示:

My Request Body looks like:

{
  "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 --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

其他故障排除提示:

  • Postman 会自动添加一些标头,例如 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天全站免登陆