意外错误400:Bad Request HttpBuilder POST Request

monal86

我正在使用以下代码将JSON对象发布到URL常规:

def http = new HTTPBuilder( 'myURL' )

// perform a POST request, expecting JSON response data
http.request( POST, JSON ) {
uri.path = myPath
uri.query = [ service_key:'1.0', event_type: 'trigger' ]
headers.'Content-Type' = 'application/json'

// response handler for a success response code:
response.success = { resp, json ->
println resp.status

// parse the JSON response object:
 json.responseData.results.each {
 ret = json.getText()
 println 'Response data: -----'
 println ret
 println '--------------------'
}
}

// handler for any failure status code:
response.failure = { resp ->
println "Unexpected error: ${resp.status} : ${resp.statusLine.reasonPhrase}"
}
}

  Ajax Code that works:(EDITED)
 $.ajax({       url:'https://events.pagerduty.com/generic/2010-04-15/create_event.json',                                                                       
            type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({

              service_key: "1379ca7018e94343bf5fa5add9fa42eb",
                 incident_key: "srv01/HTTP",
                event_type: "trigger",
                description: "TEst Test"
     }),
     dataType:'json'
     });
     alert('Message Sent');

每次遇到意外错误:400:错误的请求,如果我使用$ .ajax()执行此操作,它也会起作用。我在响应中得到HTTP:200 OK。这是怎么回事?

谢谢你。

菲尔·帕克

在ajax示例中,您要传递4个元素作为JSON主体,最终将像这样:

{"service_key": "1379ca7018e94343bf5fa5add9fa42eb",
 "incident_key": "srv01/HTTP",
 "event_type": "trigger",
 "description": "TEst Test"}

但是在常规示例中,您仅传递了两个查询字符串参数(将在uri上传递)。

您可能应该更换

uri.query = [ service_key:'1.0', event_type: 'trigger' ]

和:

body =  [service_key:'1.0', incident_key: "srv01/HTTP", event_type: 'trigger' description: "TEst Test"]

您还应该在故障响应处理程序中输出响应数据,因为许多服务将为您提供说明您不遵守服务合同的原因。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

意外错误400:错误请求HttpBuilder POST请求

来自分类Dev

如何修复 .Net Core POST 操作中的 400 Bad Request 错误?

来自分类Dev

POST http://localhost:3000/signin 400 (Bad Request) in react

来自分类Dev

使用python request.post函数时请求错误400

来自分类Dev

错误的Request -Post方法-JSON DateTime问题

来自分类Dev

烧瓶socketio 400(BAD REQUEST)

来自分类Dev

烧瓶socketio 400(BAD REQUEST)

来自分类Dev

@PathVariable 给出 400 Bad Request

来自分类Dev

禁用“ 400 Bad Request”错误,因为表单中缺少键

来自分类Dev

使用Python Socket GET请求获取400 Bad Request错误

来自分类Dev

Unity 在 SAP Rest API 上调用 HTTP-Post 时发生 400 Bad Request

来自分类Dev

MultiValueDictKeyError / request.POST

来自分类Dev

PHP $ _POST,$ _ GET或$ _REQUEST

来自分类Dev

Flask REST POST请求基于json的长度失败“ Bad Request”

来自分类Dev

AutoValidateAntiforgeryToken 总是导致 400 Bad Request 返回

来自分类Dev

postStream↵'bool'对象的类型错误不可迭代↵↵Request方法:POST↵Request

来自分类Dev

码头在格式不正确的HTTP POST标头上返回“ HTTP / 1.1 400 Bad Request”。这是预期的吗?

来自分类Dev

码头在格式不正确的HTTP POST标头上返回“ HTTP / 1.1 400 Bad Request”。这是预期的吗?

来自分类Dev

使用Postman MERN Stack在Post Request上收到404错误

来自分类Dev

替代request.post函数?

来自分类Dev

POST request with AFNetworking 2.0 - AFHTTPSessionManager

来自分类Dev

making POST request using scrapy

来自分类Dev

Http Post Request停止过程

来自分类Dev

使用Apache / NginX部署时出现Django Bad Request(400)错误

来自分类Dev

android 上的谷歌翻译 api 给出错误“GoogleJsonResponseException: 400 Bad Request”

来自分类Dev

如何忽略错误而不是返回 400 Bad Request? - Django Rest Framework

来自分类Dev

远程服务器返回错误 (400) Bad Request,状态为 ProtocolError

来自分类Dev

How to construct an HTTP post with Groovy HTTPBuilder RESTClient

来自分类Dev

nodejs request returning null body from server for a POST request

Related 相关文章

  1. 1

    意外错误400:错误请求HttpBuilder POST请求

  2. 2

    如何修复 .Net Core POST 操作中的 400 Bad Request 错误?

  3. 3

    POST http://localhost:3000/signin 400 (Bad Request) in react

  4. 4

    使用python request.post函数时请求错误400

  5. 5

    错误的Request -Post方法-JSON DateTime问题

  6. 6

    烧瓶socketio 400(BAD REQUEST)

  7. 7

    烧瓶socketio 400(BAD REQUEST)

  8. 8

    @PathVariable 给出 400 Bad Request

  9. 9

    禁用“ 400 Bad Request”错误,因为表单中缺少键

  10. 10

    使用Python Socket GET请求获取400 Bad Request错误

  11. 11

    Unity 在 SAP Rest API 上调用 HTTP-Post 时发生 400 Bad Request

  12. 12

    MultiValueDictKeyError / request.POST

  13. 13

    PHP $ _POST,$ _ GET或$ _REQUEST

  14. 14

    Flask REST POST请求基于json的长度失败“ Bad Request”

  15. 15

    AutoValidateAntiforgeryToken 总是导致 400 Bad Request 返回

  16. 16

    postStream↵'bool'对象的类型错误不可迭代↵↵Request方法:POST↵Request

  17. 17

    码头在格式不正确的HTTP POST标头上返回“ HTTP / 1.1 400 Bad Request”。这是预期的吗?

  18. 18

    码头在格式不正确的HTTP POST标头上返回“ HTTP / 1.1 400 Bad Request”。这是预期的吗?

  19. 19

    使用Postman MERN Stack在Post Request上收到404错误

  20. 20

    替代request.post函数?

  21. 21

    POST request with AFNetworking 2.0 - AFHTTPSessionManager

  22. 22

    making POST request using scrapy

  23. 23

    Http Post Request停止过程

  24. 24

    使用Apache / NginX部署时出现Django Bad Request(400)错误

  25. 25

    android 上的谷歌翻译 api 给出错误“GoogleJsonResponseException: 400 Bad Request”

  26. 26

    如何忽略错误而不是返回 400 Bad Request? - Django Rest Framework

  27. 27

    远程服务器返回错误 (400) Bad Request,状态为 ProtocolError

  28. 28

    How to construct an HTTP post with Groovy HTTPBuilder RESTClient

  29. 29

    nodejs request returning null body from server for a POST request

热门标签

归档