R httr 包 - 使用 body 向 API 发出 POST 请求
R httr package - Make POST request to an API with body
我尝试向我的 API 发出 post 请求。在 Postman 中它看起来像这样:
POST:
http://localhost:5000/api/login
body:
{"secret": "bla"}
这给了我一个可以正常工作的访问令牌。我像这样对 httr
包进行同样的尝试:
login <- list(
secret = "bla"
)
httr::POST("http://localhost:5000/api/login", body = login, verbose(), content_type("application/json"))
这是详细的输出,我不知道为什么会返回 400 错误。
-> POST /api/login HTTP/1.1
-> Host: localhost:5000
-> User-Agent: libcurl/7.64.1 r-curl/4.3 httr/1.4.1
-> Accept-Encoding: deflate, gzip
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Length: 145
-> Content-Type: application/json; boundary=------------------------a4dc23f1d3e7221e
->
>> --------------------------a4dc23f1d3e7221e
>> Content-Disposition: attachment; name="secret"
>>
>> bla
>> --------------------------a4dc23f1d3e7221e--
<- HTTP/1.0 400 BAD REQUEST
<- Content-Type: application/json
<- Content-Length: 91
<- Access-Control-Allow-Origin: *
<- Server: Werkzeug/1.0.0 Python/3.7.6
<- Date: Tue, 13 Oct 2020 15:14:54 GMT
我做错了什么?
尝试确保正文是 json 字符向量而不是 r 列表。
httr::POST("http://localhost:5000/api/login", body = jsonlite::toJSON(login), verbose(), content_type("application/json"))
我尝试向我的 API 发出 post 请求。在 Postman 中它看起来像这样:
POST:
http://localhost:5000/api/login
body:
{"secret": "bla"}
这给了我一个可以正常工作的访问令牌。我像这样对 httr
包进行同样的尝试:
login <- list(
secret = "bla"
)
httr::POST("http://localhost:5000/api/login", body = login, verbose(), content_type("application/json"))
这是详细的输出,我不知道为什么会返回 400 错误。
-> POST /api/login HTTP/1.1
-> Host: localhost:5000
-> User-Agent: libcurl/7.64.1 r-curl/4.3 httr/1.4.1
-> Accept-Encoding: deflate, gzip
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Length: 145
-> Content-Type: application/json; boundary=------------------------a4dc23f1d3e7221e
->
>> --------------------------a4dc23f1d3e7221e
>> Content-Disposition: attachment; name="secret"
>>
>> bla
>> --------------------------a4dc23f1d3e7221e--
<- HTTP/1.0 400 BAD REQUEST
<- Content-Type: application/json
<- Content-Length: 91
<- Access-Control-Allow-Origin: *
<- Server: Werkzeug/1.0.0 Python/3.7.6
<- Date: Tue, 13 Oct 2020 15:14:54 GMT
我做错了什么?
尝试确保正文是 json 字符向量而不是 r 列表。
httr::POST("http://localhost:5000/api/login", body = jsonlite::toJSON(login), verbose(), content_type("application/json"))