有人可以解释获取请求,特别是http标头吗?

问题描述 投票:0回答:1

我是休息API的新手,我正在尝试设置OAuth握手,我需要帮助来请求请求令牌。我在Python中使用requests_oauthlib模块。这是示例代码,它返回Response [400]。

consumer_key,consumer_secret和request_url都已正确加载。我让我的代码使用不同的Auth模块工作。有人可以解释什么是http标头以及它们在GET请求中的使用方式?

from requests_oauthlib import OAuth1
from variables import *

oauth = OAuth1(consumer_key, client_secret = consumer_secret)

request_token = requests.get(request_url, auth=oauth, params={'oauth_callback':'oob', 'format':'json'})

print request_token
python oauth get http-headers python-requests
1个回答
0
投票

请求:您的计算机通常在端口44380上向另一台计算机发送http消息

响应:服务器侦听任何连接请求,并在它理解消息时作出响应。

例如,你可以输入telnet stackoverflow.com 80

GET /questions/52350391/can-someone-explain-get-requests-specifically-the-http-header HTTP/2
Host: stackoverflow.com
User-Agent: curl/7.54.0
Accept: */*

然后按两次Enter键以结束请求标头,此时服务器响应:

➜  mysite telnet stackoverflow.com 80
Trying 151.101.1.69...
Connected to stackoverflow.com.
Escape character is '^]'.
GET /questions/52350391/can-someone-explain-get-requests-specifically-the-http-  header HTTP/2
Host: stackoverflow.com
User-Agent: curl/7.54.0
Accept: */*

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https://stackoverflow.com/questions/52350391/can-someone-explain-get-requests-specifically-the-http-
X-Request-Guid: xxx
Content-Security-Policy: upgrade-insecure-requests
Accept-Ranges: bytes
Age: 0
Content-Length: 217
Accept-Ranges: bytes
Date: Sun, 16 Sep 2018 03:29:16 GMT
Via: 1.1 varnish
Age: 0
Connection: close
X-Served-By: cache-ord1744-ORD
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1537068557.736123,VS0,VE25
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=xxx; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://stackoverflow.com/questions/52350391/can-someone-explain-get-requests-specifically-the-http-">here</a>.</h2>
</body></html>
Connection closed by foreign host.

然后,telnet会话打印出来自服务器的响应并关闭连接。响应将包括几个部分,响应标头和响应正文。

您的示例可能类似于:

GET /some/oauth/api?oauth_callback=oob&format=json
Host: someplace.com
Authorization: Bearer asdfasdfasdfasdf

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache

{
  "access_token":"sdfasasdfasdf",
  "token_type":"bearer",
  "expires_in":3600,
  "refresh_token":"asdfasdfasdfasdf",
  "scope":"create"
}

还可以看看:curl -Lv https://stackoverflow.com/questions/52350391/can-someone-explain-get-requests-specifically-the-http-header | head -n 100

有关:

© www.soinside.com 2019 - 2024. All rights reserved.