首页 > 解决方案 > 了解 API 响应正文 - Plumber 和 Python

问题描述

我试图了解 API 响应体到底是什么。与标题,状态代码有何不同。我想对传输的位或返回的字符串有非常低的理解。

这是我在 Plumber API 中所做的:

#* @param msg The message to echo back.
#* @get /echo
function(msg="", res){
  res$status <- 200
}

现在当我用 Python 检查它时

import requests
r = requests.get(url = 'myurl')
r.text

输出:

'[200]'

r.status_code

返回:

200

看起来响应的主体是'[200]'

现在我更改我的 API 以返回:

#* @param msg The message to echo back.
#* @get /echo
function(msg="", res){
  res$status <- 200
  res$body <- "This is body of the response"
}

在python中我得到:

r = requests.get(url = 'myurl')
r.text

输出:

'["This is body of the response"]'

r.status_code

返回

200

那么,在第二种情况下200不再是身体的一部分?那么响应的具体内容是什么。

标签: pythonapiresponseplumber

解决方案


推荐阅读